Windows form and filesystem watcher threading issue
-
Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas
-
Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas
Must say I'm chuffed - I figured out the problem by reading just the subject. :-O I had the same issue in a different context the other day; the problem is that Winforms controls are not thread safe. I'm not at work right now, so I don't have the code, but you will have to use a delegate in your form class to point to the method that actually does the work. In your event handler, you will have to invoke the delegate like
this.Invoke(new YourDelegate(TheMethodThatUpdatesTheControls));
The copy of MSDN on my colleague's machine was very helpful with this. If you're still having problems, just yell and I will give you the code when I get back to work tomorrow.
Cheers, विक्रम
And sleep will come, it comes to us all And some will fade and some will fall
-
Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas
Check out the 'usual solution' described in this article: http://www.codeproject.com/useritems/ISinchronizedInvoke.asp[^]
Cheers, विक्रम
And sleep will come, it comes to us all And some will fade and some will fall
-
Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas
You might want to read this article. Hopefully it will clear up a lot of things: http://www.codeproject.com/csharp/begininvoke.asp
-
Must say I'm chuffed - I figured out the problem by reading just the subject. :-O I had the same issue in a different context the other day; the problem is that Winforms controls are not thread safe. I'm not at work right now, so I don't have the code, but you will have to use a delegate in your form class to point to the method that actually does the work. In your event handler, you will have to invoke the delegate like
this.Invoke(new YourDelegate(TheMethodThatUpdatesTheControls));
The copy of MSDN on my colleague's machine was very helpful with this. If you're still having problems, just yell and I will give you the code when I get back to work tomorrow.
Cheers, विक्रम
And sleep will come, it comes to us all And some will fade and some will fall
-
Hi Thanks a lot for the help. Used the invoke method and it works like a charm! Kind regards Thomas
-
tsiv wrote:
Used the invoke method and it works like a charm!
It's not a charm. The behavior and solution are well documented. If you want to be a software developer rather than a snake charmer I strongly suggest you start reading the documentation.
Don't you have something better to do than drop stupid comments on this forum!? Fool. I had a problem - Vikram was kind enough to point me in the direction of a solution - why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that... Thomas
-
Don't you have something better to do than drop stupid comments on this forum!? Fool. I had a problem - Vikram was kind enough to point me in the direction of a solution - why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that... Thomas
tsiv wrote:
why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that...
Of course you don't get it because you don't comprehend the value of reading the documentation. I can point that out to people like you but I can't make you understand it. There is an old proverb, once again I can recite it but I can't make you understand it: “Give a man a fish and you feed him for a day, teach a man to fish, and you feed him for a lifetime.”