当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

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

 

关于线程的参数(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++)

本新闻共2页,当前在第1页  1  2  

相关内容
赞助商链接