Threading problem...i didn't even know I was using threads

C

Cliff

I have a Class FWControl defined in FWControl.dll (a .net class
library). It doesn't inherit anything other than Object.

FWControl has an event StatusChanged, which is actually raised in
response to a file changing. The File is being watched with
FileSystemWatcher

I have an application FWMonitor which has a reference to FWControl.dll
and uses the FWControl Class.

The FWmonitor Application Subscribes to the StatusChanged Event.

In the implementation of OnStatusChanged, I have to change some GUI
Objects. (modify their enabled property)

When I do, I get and exception: "cross thread operation not valid"

For the time being I've used CheckForIllegalCrossThreadCalls to turn
off the error.

but i'd really like to know;

a: how to do this properly....I thought this is what events were for!

b: Why is this running in multiple threads anyway. I would have
expected the whole thing to run in a single thread.

Thanks in advance

Cliff Dabbs
 
C

Cliff

Ok, so after a bit more research I found the microsoft article that
goes through this....

it uses InvokeRequired which seems sensible...only the objects I'm
trying to control don't have an implementation of that.

I'm trying to set the "Enabled" property of a ToolStripMenuItem which
isn't inherited from Control which is where InvokeRequired comes from.

help....!

Cliff.
 
D

Dick Grier

Hi,

Call a Delegate from the event. The delegate will handle the UI update (put
your code inside this routine). ControlName.Invoke or
ControlName.BeginInvoke syntax to start the delegate routine on the UI
(STATThread) thread.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
G

Guest

The menu item is associated with a menu strip which is a type of control.
You just need to find the parent MenuStrip and use that MenuStrip's
InvokeRequired/BeginInvoke. You can use anonymous methods to avoid having to
create a new method to simply re-call your event handler. For example:
private void toolStripMenuItem_SomeEvent(object sender, EventArgs e)
{
ToolStrip toolStrip = toolStripMenuItem.GetCurrentParent();
// TODO: object validation
if (toolStrip.InvokeRequired)
{
toolStrip.BeginInvoke((MethodInvoker)delegate() {
this.toolStripMenuItem_SomeEvent(sender, e); });
}
}

Unless you know you have to use Control.Invoke, it's safer to use
Control.BeginInvoke to avoid race condition deadlocks.
 
G

Guest

Or you could use the InvokeRequired and BeginInvoke on the Form where the
ToolstripMenuItem is located. It is not necessary to use the exact same
control, as all controls on a form are on the same UI thread as the form.

Joris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top