|
-
Jan 23rd, 2008, 02:27 PM
#1
[2005] Interesting Cross thread situation
I have a Background Worker doing some work, when the code starts processing the next file I have it show the name in a StatusStripLabel on the StatusStrip. This works fine, I don't need to check if an Invoke is required or anything (The control actually doesn't have the Invoke stuff in it like labels, forms, textboxes do)
Out of the blue when the background worker was running, I minimized the window. When I resorted the window half a minute later, the application crashed and gave me a 'Cross thread violation' exception. Now my question is: How do I use a delegate sub to check if an Invoke is required on a control that is thread safe, but will still throw a cross thread error?
Or, is it thread safe to check Me.WindowState without invoking it before assigning a value to the text property of the StatusStripLabel?
Thoughts? Opinions?
-
Jan 23rd, 2008, 02:46 PM
#2
Re: [2005] Interesting Cross thread situation
You shouldnt generally need to do any invoking when using the BackgroundWorker because its designed so you should call ReportProgress and do all control interaction in the ProgressChanged event handler.
If you still need to use invoke here, try calling the StatusStrips invoke method.
-
Jan 23rd, 2008, 04:19 PM
#3
Re: [2005] Interesting Cross thread situation
The ReportProgress method doesn't let me pass a string, it only accepts and integer (the percent)
I'll see about Invoking the status strip itself
-
Jan 23rd, 2008, 04:22 PM
#4
Re: [2005] Interesting Cross thread situation
 Originally Posted by JuggaloBrotha
The ReportProgress method doesn't let me pass a string, it only accepts and integer (the percent)
Also the ToolStripStatusLabel doesn't have an Invoke method, it doesn't have an InvokeRequired either
Actually, ReportProgress has a second overload that accepts 2 arguments; an integer and an object.
If you look at my last post you see that I suggested to call Invoke on the StatusStrip control, not the StatusStripLabel
-
Jan 23rd, 2008, 04:37 PM
#5
Re: [2005] Interesting Cross thread situation
 Originally Posted by Atheist
Actually, ReportProgress has a second overload that accepts 2 arguments; an integer and an object.
Didn't see the 'object' part, I really should be using a larger monitor here
Thanks for the help, invoking the status strip didn't sound like a fun idea.
-
Jan 23rd, 2008, 08:50 PM
#6
Re: [2005] Interesting Cross thread situation
You might want to have a quick look at my BackgroundWorker example code. Just follow the link in my signature.
Also, even if you're using a BackgroundWorker you'll still need to use delegation sometimes. ReportProgress/ProgressChanged is fine if you want to do the same thing every time, but if you want to be able to do a few different things on the UI from the background thread then it becomes cumbersome.
The reason that the ToolStripStatusLabel has no Invoke method is that it isn't a control. Invoke is inherited from the Control class, so the StatusStrip class DOES have an Invoke method.
The issue with cross-thread calls to controls is accessing the Handle. Presumably the Handle of your StatusStrip was not accessed until you minimised and restored the form.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|