In my WinForms c# applicat开发者_StackOverflowion I've created a method, which starts when a .mp4 file is created in a specified folder. To do this I run the following code:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Filter = "*.mp4";
watcher.Path = @"D:\transcoderen";
watcher.Created += new FileSystemEventHandler(watcher_FileCreated);
watcher.EnableRaisingEvents = true;
And eventually the method:
void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
pictureBox1.Visible = false;
}
This code works almost completely. When a .mp4 file is created in D:\transcoderen, the method starts. But for some unknown reason the application stops running when I change visible states of a control, in my example a pictureBox control. When I'm debugging in Visual Studio 2010, it just stops when it hits:
pictureBox1.Visible = false;
Visual Studio 2010 doesn't give an error or anything. It acts the same as when i click the "Stop debugging" button. Has anybody encountered this problem before? If you do so, is there a work around to achieve this?
I think it gets stuck for some reason, maybe it has something to do with the fact that this method watches for files.
Thanks in advance,
Danny
If I am not mistaken the FileWatcher event is raised from a different thread. You have to check if the pictureBox1 requires an Invoke using Control.InvokeRequired Property and then call trol.BeginInvoke Method (Delegate)
Also have a look at How to: Make Thread-Safe Calls to Windows Forms Controls
精彩评论