C#: Calling method in another thread [duplicate]
Possible Duplicate:
How to update GUI from another thread in C#?
Following scenario: I have a class with some GUI elements (winforms). This class has a update method which changes things on the controls. I also have a FileSystemWatcher. This object gives me a callback whenever a file changes. In that case I call the update method.
As you might guess this makes the application crash. The reason: the callback from the FileSystemWatcher is in another thread that the one that created the controls. If I then call the update method it can't access the controls.
What is the way to fix this? Thanks!
You should call Control.Invoke
or BeginInvoke
, see in-depth reference Here
The top voted answer this this Question looks like it might do the trick:
C# Windows Forms Application - Updating GUI from another thread AND class?
精彩评论