跨线程操作无效 [英] Cross thread operation not valid

查看:106
本文介绍了跨线程操作无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好.关于此的很多文章,但我仍然不明白我的特殊问题.

我有一个带有自己的TreeControl的应用程序,其中包含任意数量的TreeNodes.

我创建了一个对象MyWord,该对象使用GetObject(,"Word.Application")并在Word退出或激活Word文档时引发事件.此事件触发应用程序中的一个方法,该方法试图在TreerControl上隐藏TreeNode,这是我得到错误的时间.

如果我中断执行,则TreeControl和TreeNode一样返回其所有属性.当我尝试刷新TreeNode时失败.

我已经阅读了有关线程的内容,但是我认为我又回到了原来的线程上.

OK. Lots written on this, but I still don''t understand my particular problem.

I have an app with my own TreeControl, which consists of any number of TreeNodes.

I have created an object MyWord which uses GetObject(, "Word.Application") and which raises events when Word quits or when a word document is activated. This event triggers a method in my application that tries to hide a TreeNode on my TreerControl, which is when I get the error.

If I break execution, the TreeControl returns all its properties as does the TreeNode. It fails when I try to Refresh the TreeNode.

I have read about threading, but I thought I was back on my original thread.

推荐答案

斯图尔特·内森(Stuart Nathan)写道:
Stuart Nathan wrote:

但我以为我又回到了原来的话题上

but I thought I was back on my original thread


如果不确定与GUI线程是否在同一线程上,为什么不尝试找出原因呢?

使用类型System.Threading.Thread.它有一个名为CurrentThread的功能,当执行该功能时,会为您提供当前正在执行的线程的托管包装器. Thread具有一个名为 ManagedThreadId [


If you aren''t sure if you''re on the same thread as the GUI thread, why don''t you try to find out?

Use the type System.Threading.Thread. It has a porperty called CurrentThread which when this is executed gives you the currently executing thread''s managed wrapper. Thread has a property called ManagedThreadId[^] which will identify the thread uniquely via an integer. Now when your application starts up just write the ManagedThreadId into a logfile with a tag GUI - Thread next to it. In the offending code add a logging statement that will write the current thread''s ManagedThreadId into another logfile. When the error occurs just compare the thread id of the one logfile with what was written to GUI-Thread logfile. If there is a difference you''ll know that you were trying to access a GUI component from a non-GUI Thread.

Regards,

Manfred


事件可能在与UI线程不同的线程中触发.但是您只能通过UI-Thread来操作UI控件:)

了解如何在UI线程中运行委托或方法:

如果您使用的是Windows窗体,请使用Control.BeginInvoke
http://msdn.microsoft.com /en-us/library/system.windows.forms.control.begininvoke(v=vs.80).aspx [ http://msdn.microsoft.com/en-us/library/system. windows.threading.dispatcher.aspx [ ^ ]

希望对您有所帮助.
The events might be fired in a different thread than the UI thread. But you can only manipulate UI controls from the UI-Thread :)

see how to run a delegate or method in UI thread:

if you are in windows forms, use the Control.BeginInvoke
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke(v=vs.80).aspx[^]

if you are on wpf, use Dispatcher.BeginInvoke
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^]

Hope it helps.


这篇关于跨线程操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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