码迷,mamicode.com
首页 > 编程语言 > 详细

使用线程操作刷新Winform界面

时间:2017-03-25 17:37:18      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:color   cat   log   orm   void   start   窗体   art   添加   

主窗体中添加代码

public FrmMain()
        {
            InitializeComponent();
            System.Threading.Thread thread = new System.Threading.Thread(Do_work);
                thread.IsBackground = true;
                thread.Start();
        }


 private void Do_work()
        {
            while (true)
            {
                //将sleep和无限循环放在等待异步的外面 
                System.Threading.Thread.Sleep(5000);
                System.Windows.Forms.Application.DoEvents();
                ThreadFuntion();
            }

        }

 private void ThreadFuntion()
        {
            if (this.label_State.InvokeRequired)
            {
                //1 通常方法:将代理绑定到方法  
                FlushClient fc = new FlushClient(ThreadFuntion);
                this.BeginInvoke(fc);//通过代理调用刷新方法  
            }
            else
            {
                label_State.Text = "程序已经运行5秒钟了";
            }
        }

 

使用线程操作刷新Winform界面

标签:color   cat   log   orm   void   start   窗体   art   添加   

原文地址:http://www.cnblogs.com/step-city/p/6617632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!