跨线程问题 [英] Problems with cross-threading
问题描述
我有一个具有以下功能的应用程序:-
如果不存在数据文件,请读取文件夹中的文件并创建一个新的数据文件.
读取数据文件.
显示其内容.
它还具有一个文件夹监视程序,如果它检测到文件夹中新创建的文件,则会引发一个事件.
此事件读取此文件并将其详细信息添加到数据文件中.
但是现在的问题是:
然后,我尝试重新显示其内容.
我遇到了跨线程问题.显然引发事件会创建一个新线程.
我尝试使用Delegate方法,但是功能太多,并且代码(我认为)会造成混乱.
我以为可以省略显示例程,而是在表单上放置一个按钮以重新显示它,以为通过按该按钮,我将回到原来的线程中.是的,没有.通常是的,但是一个重要的类不再被初始化,所以我可以回到原来的线程上吗?
我在VS2008中使用Visual Basic
感谢您提供任何想法.
I have an application that has these functions:-
If no data file exists, read the files in a folder and create a new data file.
Read the data file.
Display its contents.
It also has a folder watcher which if it detects a newly created file in the folder, raises an event.
This event reads this file and adds it''s details to the data file.
But now the problem:
I then try to re-display its contents.
I get a cross thread problem. Obviously raising the event creates a new thread.
I tried using the Delegate method, but there are too many functions, and the code will (I think) get too confusing.
I thought that I could omit the display routine, and put a button on the form to re-display it, thinking that by pressing the button, I would be back in the original thread. Yes and no. Mostly yes, but an important class no longer initialised, so can I be back on the original thread?
I am using Visual Basic in VS2008
Thanks for any ideas.
推荐答案
这就是问题所在.
System.Timer创建一个不同的线程.
This was the problem.
System.Timer creates a different thread.
这篇关于跨线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!