更新主窗口控件的内容2:子线程工作时同时更新主线程内的控件内容

 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.Windows.Forms;
 9 using System.Threading;
10 
11 namespace WindowsFormsApplication1
12 {
13     public partial class MainForm : Form
14     {
15         MainFormHelper mh = new MainFormHelper();
16         public MainForm()
17         {
18             InitializeComponent();
19 
20             mh.SetMsgInfoEvent += new MainFormHelper.SetMsgInfoHandler(mh_SetMsgInfoEvent);
21         }
22 
23         void mh_SetMsgInfoEvent(string msg)
24         {
25             this.textBox1.Text = msg; 
26             this.textBox1.Update();
27         }
28 
29         /// <summary>
30         /// 入口
31         /// </summary>
32         /// <param name="sender"></param>
33         /// <param name="e"></param>
34         private void button1_Click(object sender, EventArgs e)
35         {
36             mh.Begin();
37         }
38     }
39 
40     public class MainFormHelper
41     {
42         public delegate void SetMsgInfoHandler(string msg);
43         public event SetMsgInfoHandler SetMsgInfoEvent;
44 
45         public void Begin()
46         {
47             if (SetMsgInfoEvent != null)
48             {
49                 for (int i = 0; i < 10; i++)
50                 {
51                     SetMsgInfoEvent(i.ToString());
52                     System.Threading.Thread.Sleep(500);
53                 }
54             }
55         }
56     }
57 }

 

转载于:https://www.cnblogs.com/pnljs/p/3623015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值