跨线程异常行为有所不同 [英] Cross Thread exception behavior differs

查看:72
本文介绍了跨线程异常行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

我只是想知道为什么我们在Winform应用程序中访问UI元素时会遇到跨线程异常,而我们在找不到相同的行为时访问会员字段?。我怀疑是因为像文本框这样的UI元素和像整数一样的成员变量都是Form类的成员,并且它们都应该由同一个线程创建,为什么它们被区别对待?根据我的理解默认情况下,winform应用程序中只有一个线程。



任何帮助都将不胜感激。

Hello All,
I was just curious to know why we get Cross thread exception when we access an UI element in Winform application whereas we don''t find the same behavior when we access an member field?. My doubt is that since both UI element like a textbox and a member variable like an integer are members of Form class and they both should be created by the same thread, why is that they are treated differently?. As per my understanding by default there will be only one thread in a winform application.

Any help would be appreciated.

推荐答案

你不能从非UI调用与UI相关的任何内容线。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



重要:请不要使用提交您的解决方案发表评论,该解决方案仅适用于您在回答问题时提供帮助的情况。使用添加评论或回复现有评论。



-SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Important: Please don''t comment using "Submit your solution", which is reserved for the cases when you provide some help in response to a question. Use "Add comment" or "Reply" to existing comment.

—SA


@ Harsha



如果您在相同的UI线程(即主线程)中访问控件,则永远不会遇到跨线程异常。



只有当您尝试在主线程以外的其他线程中访问UI控件时才会出现此异常。辅助线程可以是从不同线程或使用ThreadPool调用的Delegate方法。



因为你说你得到交叉线程异常你可以将你的代码粘贴到它抛出的地方这个例外吗?从您的消息中明确表示您正在尝试访问主线程内的控件。但是分享代码以便更好地理解你身边发生的事情会很好。
@Harsha

You never get a cross thread exception if you are accessing the Controls in the same UI thread i.e your main thread.

you get this exception only when you try to access the UI Control in a different thread other than Primary thread. The secondary thread can be a Delegate method you invoked from a different thread or using ThreadPool.

since you said you get Cross Thread Exception can you paste your code where it throws this exception? from your message Its clear that you are trying to access the control inside the main thread. But it would be nice to share the code for the better understanding of what is happening at your side.


harsha,



将线程对象作为类的成员并不意味着它在与UI线程相同的线程下运行。此代码清楚地解释了您从代码创建的辅助线程访问UI控件的所有内容



线程th =新线程(DoHeavyOperation);

th.Start();



这里你传递的方法只不过是委托。 Thread类创建一个与您的方法匹配的委托,并在辅助线程中执行。它将接受任何与代表签名相匹配的方法,并且不强制要求成为该类的成员。



您也可以通过



线程th =新线程(newClassInstance.DoHeavyOperation());

th.Start();



th.Start将执行newClass的DoHeavyOperation方法



希望这会清除你的dbt。?
harsha,

Having the thread object as member of the class doesnt mean that it runs under the same thread as that of UI Thread. This code clearly explains all that you are accessing the UI control from a secondary thread created by the code

Thread th = new Thread(DoHeavyOperation);
th.Start();

here you are passing a method is nothing but the delegate. The Thread class creates a delegate that matches your method and executes in the secondary thread. It will accept any method which matches the delegate signature and its not mandatory to be the member of this class.

you can also pass
i.e
Thread th = new Thread(newClassInstance.DoHeavyOperation());
th.Start();

th.Start will execute the method DoHeavyOperation of newClass

hope this will clear your dbt.?


这篇关于跨线程异常行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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