c#中跨线程使用控件

c#跨线程使用控件需要使用委托来实现

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 using System.Xml;
12 
13 namespace csharp_xmlTransPortTest
14 {
15     public partial class Form1 : Form
16     {
17         public delegate void ThreadProcess();
18         public Form1()
19         {
20             InitializeComponent();
21            
22 
23         }
24 
25         XmlDocument statusDoc = new XmlDocument();
26         public void xx()
27         {
28 //             statusDoc.Load(@"D:\status.xml");
29 //             string status = statusDoc.SelectSingleNode("/root/status").InnerText;
30 // 
31 //             
32 //             char[] ch = new char[status.Length];
33 //             int i = 0;
34 //             foreach (char c in status)
35 //             {
36 //                 ch[i] = c;
37 //                 if (c == '1')
38 //                 {
39 //                     ch[i] = '0';
40 //                 }
41 //                 i++;
42 //             }
43 //             string update = new string(ch);
44 //             statusDoc.SelectSingleNode("/root/status").InnerText = update;
45 //             statusDoc.Save(@"D:\status.xml");
46 
47             thread0 = new Thread(new ThreadStart(startedcross));
48             thread0.Start();
49             thread1 = new Thread(new ThreadStart(started1cross1));
50             thread1.Start();
51         }
52 
53         private Thread thread0;
54         private Thread thread1;
55 
56         private void startedcross()
57         {
58             ThreadProcess tp = new ThreadProcess(started);
59             tp.BeginInvoke(null, null);
60         }
61 
62         private void started1cross1()
63         {
64             ThreadProcess tp1 = new ThreadProcess(started1);
65             tp1.BeginInvoke(null, null);
66         }
67         private void started()
68         {
69             while (true)
70             {
71                 //string str = "32";
72                 object str = new object();
73                 lock (str)
74                 {
75                     this.textBox1.Text += "0000000000000\n";
76                 }
77             }
78         }
79 
80         private void started1()
81         {
82             while (true)
83             {
84                 object lockthis = new object();
85                 lock (lockthis)
86                 {
87                     this.textBox1.Text += "11111111111\n";
88                 }
89             }
90         }
91 
92         private void button1_Click(object sender, EventArgs e)
93         {
94              xx();
95         }
96  
97     }
98 }
原文地址:https://www.cnblogs.com/jck34/p/2829722.html