c#中跨線程使用控制項

來源:互聯網
上載者:User

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_xmlTransPortTest14 {15     public partial class Form1 : Form16     {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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.