当前位置:编程学习 > C#/ASP.NET >>

无法捕获CrossThreadMessagingException异常信息

场景:在winform的多线程编程中创建的子线程直接获取窗体中一个栏位的值
问题:可获得该栏位的值,但鼠标放置VS2010中该栏位上时可见异常提示信息“An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred”,但try……catch中无法捕获此异常。

原页面布局:


代码:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.txtBoxMain.Text = "主线程";

            Thread subThread = new Thread(new ThreadStart(GetValue));
            subThread.Start();
        }

        private void GetValue()
        {
            Thread.Sleep(5000);
            try
            {
                string testGetValue = this.txtBoxSub.Text;
            }
            catch (Exception exc)
            {
            }
        }
    }

GetValue函数可以正常运行,testGetValue 变量可以正常获取到值,没有任何异常出现,但在调试断点的时候鼠标放置“this.txtBoxSub.Text”的“Text”上可看见异常信息“An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred”,具体如下图:


请各位大拿不惜赐教,出现不能正常捕获此异常的原因是什么?多谢!

附:我已将VS的Exception全部勾选,如下图:
C# 异常信息不能正常捕获 --------------------编程问答-------------------- 因为checkForIllegalCrossThreadCalls只有在debugger加载下才开启,类似:
public class Control
{
   static Control()
   {
     checkForIllegalCrossThreadCalls = Debugger.IsAttached;   
   }
}


如果没有加载调试器,就没有检查非法跨线程调用,程序就‘正常’运行。
这里的‘正常’指的是没有CrossThreadMessagingException,但不保证正确的同步。
--------------------编程问答-------------------- 学习了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,