Interesting Cross thread situation

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
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?
 
I get Cross-thread exception right away when accessing a StatusStripLabel from DoWork handler. Why don't you use the ProgressChanged event, it's "safe" :) You don't need a InvokeRequired property if you know you have to invoke across threads. BGW.DoWork runs asynchronous so you have to from there.
You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the BackgroundWorker events.
StatusStripLabel is a component, only controls have the InvokeRequired property, like its parent the StatusStrip.
 
The ReportProgress method (ProgressChanged event) was the way to go

When I started threading in VS 2003, I finally got the hang of it 2 weeks before I got bumped up to VS 2005, and the background worker has been saving me time since then too, I just wish they'd given me more time back then to get to know the background worker.
 
Back
Top