当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 关于线程的参数、“返回值”、及线程的中止

ASP.NET
合理的网盟广告策略:如何规划与投放网盟广告
GoDaddy Backorder域名抢注经验分享
Google Analytics获得GOOGLE真正的收录网站数据指标
Visual Studio 2008 Team Suite简体中文正式版- 激活方法

ASP.NET 中的 关于线程的参数、“返回值”、及线程的中止


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 54 ::
收藏到网摘: n/a

 

关于线程的参数(2.0)、“返回值”、及线程的中止


1.线程的参数:
有时候会想向辅助线程传递些信息,这里需要用到ParameterizedThreadStart 委托

示例:

        private void btRunThread_Click(object sender, EventArgs e)

        {

            Thread t = new Thread(new ParameterizedThreadStart(this.ThreadRun));

            t.Start(100);

        }

 

        private void ThreadRun(object o)

        {

            this.lbCompleted.Invoke((MethodInvoker)delegate { this.lbCompleted.Text = System.Convert.ToString(o); });

        }

 

2.通过代理可以大致实现类似功能,示例:

    class Program

    {

        static void Main(string[] args)

        {

            ThreadClass tc = new ThreadClass(new MyDlg(DlgMethod));

            Thread thread = new Thread(new ThreadStart(tc.ThreadRun));

            Console.WriteLine("second thread start");

            thread.Start();

            thread.Join();

            Console.WriteLine("second thread completed");

            Console.Read();       

        }


        private static void DlgMethod(int i)

        {

            Console.WriteLine("Second Thread Result:{0}", i);

        }

    }

 

    public delegate void MyDlg(int i);

 

    class ThreadClass

    {

        private MyDlg myDlg;

 

        public ThreadClass(MyDlg pDlg)

        {

            this.myDlg = pDlg;

        }

 

        public void ThreadRun()

        {

            int total = 0;

            for (int i = 0; i < 100; i++)

            {

                total += i;

            }

 

            if (myDlg != null)

            {

                myDlg(total);

            }

        }