线程间操作无效-创新互联

方法一:

站在用户的角度思考问题,与客户深入沟通,找到柳河网站设计与柳河网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站建设、企业官网、英文网站、手机端网站、网站推广、域名与空间、虚拟主机、企业邮箱。业务覆盖柳河地区。

public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

方法二:

      using System;
 using System.Threading;
 using System.Windows.Forms;

 namespace TestingUIThread
 {
    public partial class Form1 : Form
    {
        Thread thread = null;

        public Form1()
        {

            InitializeComponent();
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ParameterizedThreadStart(EnableButton));
            thread.Start(null);
        }

        private void EnableButton(object o)
        {
            EnableButton();
        }

        private delegate void EnableButtonCallBack();

        private void EnableButton()
        {
            if (this.btnTest.InvokeRequired)
            {
                this.Invoke(new EnableButtonCallBack(EnableButton));
            }
            else
            {
                btnTest.Enabled = true;
                btnTest.Text = "Enabled";
            }
        }
    }
 }

方法三: 通过ISynchronizeInvoke和MethodInvoker.

using System;
 using System.ComponentModel;
 using System.Threading;
 using System.Windows.Forms;

 namespace TestingUIThread
 {
    public partial class Form1 : Form
    {
        Thread thread = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ParameterizedThreadStart(MyEnableButton));
            thread.Start(null);
        }

        private void MyEnableButton(object sender)
        {
            ISynchronizeInvoke synchronizer = this;

            if (synchronizer.InvokeRequired)
            {
                MethodInvoker minvoker = new MethodInvoker(this.EnableButton);
                synchronizer.Invoke(minvoker, null);
            }
            else
            {
                this.EnableButton();
            }
        }

        private void EnableButton()
        {
            btnTest.Enabled = true;
            btnTest.Text = "Enabled";
        }
    }
 }

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:线程间操作无效-创新互联
本文地址:http://ybzwz.com/article/gpjjd.html