C# 如果何從線程中操作控制項

來源:互聯網
上載者:User

下面是比較早期寫的一個監控UDP接收的線例子:

 1        //申明委託
 2        public delegate void mydelegate();
 3        //添加調用控制的方法
 4        public void outtext() 
 5        {
 6            textBox1.Text = Read_str;
 7       
 8        }              
 9        public void start_server()
10        {
11            while (true)
12            {
13                //接收從遠程主機發送到本地8090連接埠的資料
14                byte[] recData = server.Receive(ref receivePoint);
15                ASCIIEncoding encode = new ASCIIEncoding();
16                //獲得用戶端請求資料
17                Read_str = encode.GetString(recData);
18                //提取用戶端的資訊,存放到定義為temp的字串數組中
19                //string[] temp = Read_str.Split("/".ToCharArray());
20                //線上程中調用對象;
21                mydelegate mydelegate = new mydelegate( outtext );
22                //BeginInvoke/invoke(同步或非同步) 方法來將調用封送到適當的線程
23                textBox1.BeginInvoke(mydelegate, new object[] { });
24 
25            }
26        }
27        public void run()
28        {
29            //利用本地8080連接埠號碼來初始化一個UDP網路服務
30            server = new UdpClient(port);
31            receivePoint = new IPEndPoint(new IPAddress(ip), port);
32            //開一個線程
33            startServer = new Thread(new ThreadStart(start_server));
34            //啟動線程
35            startServer.Start();
36            
37        }
38        private void Form1_Load(object sender, EventArgs e)
39        {
40            //啟動對時服務
41            run();
42            
43        }

參考文章:http://msdn2.microsoft.com/zh-cn/library/757y83z4(VS.80).aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.