欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#基础,单线程,跨线程访问和线程带参数

程序员文章站 2022-04-27 23:32:55
1 using System; 2 using System.Collections.Generic; 3 using System.Threading; 4 using System.Windows.Forms; 5 6 namespace 线程和跨线程 7 { 8 public partial ... ......
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Threading;
  4 using System.Windows.Forms;
  5 
  6 namespace 线程和跨线程
  7 {
  8     public partial class Form1 : Form
  9     {
 10         public Form1()
 11         {
 12             InitializeComponent();
 13         }
 14         /// <summary>
 15         /// 单线程直接假死了
 16         /// </summary>
 17         /// <param name="sender"></param>
 18         /// <param name="e"></param>
 19         private void btnAlone_Click(object sender, EventArgs e)
 20         {
 21             for (int i = 0; i < 100000; i++)
 22             {
 23                 //通过[调试]-[窗口]-[输出]显示打印值
 24                 Console.WriteLine(i);
 25             }
 26         }
 27 
 28 
 29         /// <summary>
 30         /// 新线程运行,窗体不假死
 31         /// </summary>
 32         /// <param name="sender"></param>
 33         /// <param name="e"></param>
 34         private void btnNew_Click(object sender, EventArgs e)
 35         {
 36             Thread th = new Thread(ShowCalculator)
 37             {
 38                 IsBackground = true
 39             };
 40             th.Start();
 41 
 42         }
 43         /// <summary>
 44         /// 循环计算方法,供新线程使用
 45         /// </summary>
 46         private void ShowCalculator()
 47         {
 48             for (int i = 0; i < 100000; i++)
 49             {//通过[调试]-[窗口]-[输出]显示打印值
 50                 Console.WriteLine(i);
 51             }
 52         }
 53         /// <summary>
 54         /// 带参数的
 55         /// </summary>
 56         /// <param name="sender"></param>
 57         /// <param name="e"></param>
 58         private void btnParameters_Click(object sender, EventArgs e)
 59         {
 60             List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
 61             ParameterizedThreadStart parThreadStart = new ParameterizedThreadStart(ShowParameters);
 62             Thread th = new Thread(parThreadStart) { IsBackground = true };
 63             th.Start(list);
 64         }
 65         private void ShowParameters(object obj)
 66         {
 67             //线程中的参数只能是Object
 68             List<int> result = obj as List<int>;
 69             foreach (var item in result)
 70             {
 71                 MessageBox.Show(item.ToString());
 72             }
 73         }
 74         /// <summary>
 75         /// 跨线程访问
 76         /// </summary>
 77         /// <param name="sender"></param>
 78         /// <param name="e"></param>
 79         private void button1_Click(object sender, EventArgs e)
 80         {
 81             Thread th = new Thread(ShowMulti) { IsBackground = true };
 82             th.Start();
 83         }
 84         /// <summary>
 85         /// 解决跨线程访问报异常,不使用
 86         /// </summary>
 87         private void ShowMulti()
 88         {
 89             int first = 0;
 90             for (int i = 0; i < 10; i++)
 91             {
 92                 first = i;
 93             }
 94             //是否要对lbl控件进行跨线程
 95             if (this.lblShow.InvokeRequired)
 96             {
 97                 //对委托中的数据类型验证
 98                 this.lblShow.Invoke(new Action<Label, string>(ShowLableValue), this.lblShow, first.ToString());
 99             }
100             else
101             {
102                 this.lblShow.Text = first.ToString();
103             }
104         }
105         /// <summary>
106         /// 把值写到控件中
107         /// </summary>
108         /// <param name="lbl"></param>
109         /// <param name="value"></param>
110         private void ShowLableValue(Label lbl, string value)
111         {
112             lbl.Text = value;
113         }
114 
115         private void Form1_Load(object sender, EventArgs e)
116         {
117             //关闭跨进程检查
118             //Label.CheckForIllegalCrossThreadCalls = false;
119             //改用委托方法实现
120         }
121     }
122 }