如何在文本框中显示WorkerThread异常? [英] How to show WorkerThread exception in textbox ?

查看:64
本文介绍了如何在文本框中显示WorkerThread异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在我的应用程序中使用workder线程.我希望当我的应用程序引发任何异常时,应将其填充在列表框中,并且不应中断执行流程.但它显示了此异常.

Hi,
I Am using workder thread in my application. I want that when my application raise any exception it should be populated in the listbox and flow of exectution shouldn''t be breaked out. But it showing this exception.

Cross-thread operation not valid: Control ''txtBxException'' accessed from a thread other than the thread it was created on.



Plz guide



Plz guide

推荐答案

这是从WinForms编程开始时的常见错误.
您需要执行以下操作:
This is common error when one starts with WinForms programming.
You need to do something like this:
if (txtBxException.InvokeRequired)
        txtBxException.Invoke(new MethodInvoker(
            delegate
            {
                txtBxException.Text = "Describe error here";
            }
            ));
    else
    {
        txtBxException.Text = "Describe error here";
    }


WinForms中存在一个问题,即控件只能从最初在其上创建的主(GUI)线程中进行更新.
因此,当您在后台线程上工作时,需要使用.Invoke().BeginInvoke()方法来将对主线程的控制更新推迟.


The problem there is limitation in WinForms that controls can be updated only from main (GUI) thread, on which they were originally created.
So, when you''re working on background thread you need to deffer updating control to main thread using .Invoke() or .BeginInvoke() methods.


这篇关于如何在文本框中显示WorkerThread异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆