多线程执行带参数和不带参数的方法

C#代码
  1. using System.Threading;  
  2.   
  3. namespace 多线程  
  4. {  
  5.     public partial class FormThread : Form  
  6.     {  
  7.         public FormThread()  
  8.         {  
  9.             InitializeComponent();  
  10.             TextBox.CheckForIllegalCrossThreadCalls = false;//临时关闭微软错误检查  
  11.         }  
  12.         //单线程的缺点  
  13.         private void btnSingle_Click(object sender, EventArgs e)  
  14.         {  
  15.             CountTime();  
  16.         }  
  17.         /// <summary>  
  18.         /// 计数方法  
  19.         /// </summary>  
  20.         void CountTime()  
  21.         {  
  22.             DateTime beiginTime = DateTime.Now;  
  23.             for (int i = 0; i < 999999999; i++)  
  24.             {   
  25.               
  26.             }  
  27.             TimeSpan ts = beiginTime.Subtract(DateTime.Now);  
  28.             MessageBox.Show("执行完毕" + ts.Milliseconds);     
  29.         }  
  30.         //多线程解决UI卡死  
  31.         private void btnNosingle_Click(object sender, EventArgs e)  
  32.         {  
  33.             //创建线程对象,传入线程要执行的方法  
  34.             //ThreadStart td = new ThreadStart(sayHello);  ThreadStart表示在Thread上要执行的方法  
  35.             Thread thread = new Thread(CountTime);  
  36.             //将线程设置为后台线程,当所有的前台线程结束,后台线程自动退出,  
  37.             //前台线程:只有所有的前台线程都关闭才能完成程序的关闭  
  38.             thread.IsBackground = true;  
  39.             //启动线程 执行方法  
  40.             thread.Start();  
  41.         }  
  42.         /// <summary>  
  43.         /// 修改文本框的内容  
  44.         /// </summary>  
  45.         void increase()  
  46.         {  
  47.             for (int i = 0; i < 2000; i++)  
  48.             {  
  49.                 int inc = int.Parse(txtInput.Text);  
  50.                 Console.WriteLine(Thread.CurrentThread.Name + ",i=" + i);  
  51.                 inc++;  
  52.                 txtInput.Text = inc.ToString();  
  53.             }  
  54.   
  55.         }  
  56.         //多线程方法重入问题  
  57.         private void btnMethods_Click(object sender, EventArgs e)  
  58.         {  
  59.             ThreadStart tds = new ThreadStart(increase); //返回委托类型,表示在线程上执行的方法  
  60.             Thread thd = new Thread(tds);  
  61.             thd.Name = "t1";  
  62.             thd.IsBackground = true;  
  63.             thd.Start();  
  64.   
  65.             Thread thd2 = new Thread(increase);  
  66.             thd2.Name = "t2";  
  67.             thd2.IsBackground = true;  
  68.             thd2.Start();  
  69.         }  
  70.         /// <summary>  
  71.         /// 带参数的方法  
  72.         /// </summary>  
  73.         void GetTxtName(object name)  
  74.         {  
  75.             MessageBox.Show("name="+name);  
  76.         }  
  77.         //线程执行带参数的方法  
  78.         private void btnParameters_Click(object sender, EventArgs e)  
  79.         {  
  80.             ParameterizedThreadStart phs = new ParameterizedThreadStart(GetTxtName);  
  81.             Thread thdParameters = new Thread(phs);  
  82.             thdParameters.IsBackground = true;  
  83.             thdParameters.Start(txtName.Text);  
  84.         }  
  85.         /*---------------------------------------------------------------------- 
  86.          * 总结:线程必须要传入执行的方法,可以是带参数和不带参数两类 
  87.          * 线程执行不带参数的方法使用ThreadStart 
  88.          * 线程执行带参数的方法使用ParameterizedTheadStart 
  89.          * 线程都是使用委托传入方法 
  90.         -----------------------------------------------------------------------*/  
  91.     }  
  92. }  

线程执行带多个参数的方法请参考:www.zhuzhiyong.com.cn/article/zzy_wf/316.htm



[本日志由 月神 于 2012-01-15 08:07 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 多线程
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.