C#跨线程访问控件不提示错误问题

来源:互联网 发布:微软office for mac 编辑:程序博客网 时间:2024/06/06 02:39

.net2.0以后加强了安全运行机制,不允许在winform中跨线程访问控件属性。但是本人在使用C#4.5的时候发现自己跨线程访问控件属性居然完全不提示错误。代码如下

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(ThreadFuntion);
            thread.IsBackground = true;
            thread.Start();
        }
        private void ThreadFuntion()
        {
            while (true)
            {
                this.textBox1.Text = DateTime.Now.ToString();
                Thread.Sleep(1000);
            }
        }
    }

这段代码,在编译的时候没有出现错误提示。在public Form1()构造函数中加入Control.CheckForIllegalCrossThreadCalls = true;代码后才出现错误提示,新增代码表示在编译过程中检查跨线程访问是否存在异常。

0 0
原创粉丝点击