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

winform 跨线程访问控件

时间:2017-02-21 01:08:04      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:show   window   ext   aaa   .text   null   app   draw   event   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        delegate void  DelShow(string strInput);
        DelShow delShow = null;
        public Form1()
        {
            InitializeComponent();
            delShow =new DelShow(MyShow);
            //Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t1 = new System.Threading.Thread(() => {
                if(this.InvokeRequired)
                {
                    #region 方式一
                    //this.Invoke(new Action<string>(s => { this.label1.Text = s; }),"bbb");
                    #endregion
                    #region 方式二
                    this.Invoke(delShow, "ccc");
                    #endregion
                }
                else
                {
                    this.label1.Text = "aaa";
                }
                
            });
            t1.IsBackground = true;
            t1.Start();
        }

        private void MyShow(string strInput)
        {
            this.label1.Text = strInput;
        }
    }
}

 

winform 跨线程访问控件

标签:show   window   ext   aaa   .text   null   app   draw   event   

原文地址:http://www.cnblogs.com/caohuimingfa/p/6422075.html

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