SomeDeveloperPerson

I'm trying to update a label's text from another thread. I've tried using a backgroundworker as described in the articles at "http://msdn2.microsoft.com/en-us/library/b2zk6580(vs.80).aspx" and "http://msdn2.microsoft.com/en-us/library/ms233672(VS.80).aspx". I still get an error "Cross-thread operation not valid: Control [FormName] accessed from a thread other than the thread it was created on."

I've seen the example at "http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=564275&SiteID=1" but I get the same error and it doesn't show how to cancel the operation.

I have to run a long operation in the background but have the GUI responsive, with the option to cancel the operation. Why is BackgroundWorker not working What can I do to get this to work A small example or a pointer to a complete example would be nice.

I'm using C# and .Net 2.0.


Re: Windows Forms General Access Control from Background Thread...Again...

nobugz

There is an example in that thread. Are you using Invoke()





Re: Windows Forms General Access Control from Background Thread...Again...

SomeDeveloperPerson

nobugz wrote:
There is an example in that thread. Are you using Invoke()


I used the example in the thread and got the same error. I also need to be able to cancel the operation at any time, but that thread's example doesn't show how this can be done.




Re: Windows Forms General Access Control from Background Thread...Again...

nobugz

Well, you are doing something wrong if you still get IllegalOperationException. Show us your code and indicate the line that throws the error. We'll save the second question for later.





Re: Windows Forms General Access Control from Background Thread...Again...

SomeDeveloperPerson

The Microsoft examples at "http://msdn2.microsoft.com/en-us/library/b2zk6580(vs.80).aspx" and "http://msdn2.microsoft.com/en-us/library/ms233672(VS.80).aspx" do not invoke methods on the GUI in the backgroundWorker1_RunWorkerCompleted and backgroundWorker1_DoWork methods
The only place where GUI methods are invoked is in the backgroundWorker1_ProgressChanged method.

Placing a GUI method invocation in either
backgroundWorker1_RunWorkerCompleted or backgroundWorker1_DoWork produces an error.
I haven't seen this documented anywhere but these are my observations.

There are several diverse GUI manipulations that my application must perform before, during and after the background thread completes. Those that must occur before are easy to fix, but do I have to create several delegate methods to handle the others and is this really the only way that background jobs are handled in Windows Forms

nobugz wrote:
Well, you are doing something wrong if you still get IllegalOperationException. Show us your code and indicate the line that throws the error. We'll save the second question for later.