|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
I don't get this --> Make Thread-Safe Calls to Windows Forms ControlsI am trying to run some pretty simple code that monitors a folder for
changes (copied the example at http://www.codeproject.com/dotnet/folderwatcher.asp). But, when I try and update a textbox control I keep getting the "Cross-thread operation not valid: Control 'txt_folderactivity' accessed from a thread other than the thread it was created on." error. I found the Microsoft explanation at http://msdn2.microsoft.com/en-us/library/ms171728(VS.80,d=ide).aspx, but I just don't get the explanation. Can somebody help me understand why I can't simply set the text property from a thread and what this Microsoft explanation is trying to tell me? Thanks! "ljh" <Reply@groups.please> schrieb: Set the filesystemwatcher's 'SynchronizingObject' property to the textbox.>I am trying to run some pretty simple code that monitors a folder for > changes (copied the example at > http://www.codeproject.com/dotnet/folderwatcher.asp). > > But, when I try and update a textbox control I keep getting the > "Cross-thread operation not valid: Control 'txt_folderactivity' accessed > from a thread other than the thread it was created on." error. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Sweet!
Thanks Herfried! Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:ucj6rBTkGHA.4660@TK2MSFTNGP05.phx.gbl... > "ljh" <Reply@groups.please> schrieb: >>I am trying to run some pretty simple code that monitors a folder for >>changes (copied the example at >>http://www.codeproject.com/dotnet/folderwatcher.asp). >> >> But, when I try and update a textbox control I keep getting the >> "Cross-thread operation not valid: Control 'txt_folderactivity' accessed >> from a thread other than the thread it was created on." error. > > Set the filesystemwatcher's 'SynchronizingObject' property to the textbox. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> Windows forms and controls can only be accessed from the thread they
were created on. That is the main UI thread. The FileSystemWatcher events come from some other thread. The cross-thread operation error you see is coming from a Managed Debugging Assistant (MDA) which is nice enough to warn you of the potential problem in debug builds. MDAs are not compiled into release builds. They are a new feature in 2.0. Setting the SynchronizingObject property to a Form or Control in your application tells the FileSystemWatcher to automatically marshal the execution of events onto the thread that created that form or control. ljh wrote: Show quoteHide quote > I am trying to run some pretty simple code that monitors a folder for > changes (copied the example at > http://www.codeproject.com/dotnet/folderwatcher.asp). > > But, when I try and update a textbox control I keep getting the > "Cross-thread operation not valid: Control 'txt_folderactivity' accessed > from a thread other than the thread it was created on." error. > > I found the Microsoft explanation at > http://msdn2.microsoft.com/en-us/library/ms171728(VS.80,d=ide).aspx, but I > just don't get the explanation. > > Can somebody help me understand why I can't simply set the text property > from a thread and what this Microsoft explanation is trying to tell me? > > Thanks! Thanks for the explanation....
Now I am finding out all of the GOTCHAs associated with using the FileSystemWatcher control (see the "FileSystemWatcher raises Changed Twice...." thread for all of the gory details). Show quoteHide quote "Brian Gideon" <briangid***@yahoo.com> wrote in message news:1150497582.558072.236520@i40g2000cwc.googlegroups.com... > Windows forms and controls can only be accessed from the thread they > were created on. That is the main UI thread. The FileSystemWatcher > events come from some other thread. The cross-thread operation error > you see is coming from a Managed Debugging Assistant (MDA) which is > nice enough to warn you of the potential problem in debug builds. MDAs > are not compiled into release builds. They are a new feature in 2.0. > > Setting the SynchronizingObject property to a Form or Control in your > application tells the FileSystemWatcher to automatically marshal the > execution of events onto the thread that created that form or control. > > ljh wrote: >> I am trying to run some pretty simple code that monitors a folder for >> changes (copied the example at >> http://www.codeproject.com/dotnet/folderwatcher.asp). >> >> But, when I try and update a textbox control I keep getting the >> "Cross-thread operation not valid: Control 'txt_folderactivity' accessed >> from a thread other than the thread it was created on." error. >> >> I found the Microsoft explanation at >> http://msdn2.microsoft.com/en-us/library/ms171728(VS.80,d=ide).aspx, but >> I >> just don't get the explanation. >> >> Can somebody help me understand why I can't simply set the text property >> from a thread and what this Microsoft explanation is trying to tell me? >> >> Thanks! > |
|||||||||||||||||||||||