thread problem

E

Eps

ok i have a timer object in my windows forms application, the app also
makes use of a COM object which also uses threading.

I have a seperate class that deals with COM object but this class has to
pass infomation back to the main windows form from time to time, so i
pass the main form to the class that that calls the COM object so i can
access the windows form GUI.

However this always causes errors...

A first chance exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

A first chance exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll

in the debugging window and the GUI never updates properly. I am fairly
certain it is to do with me calling the main form from the class that
deals with the COM object, probably a threading issue i think.

Anyone know how i can update my GUI from the seperate class ?

any help appreciated.
 
O

Ollie Riches

yes you are correct it is a threading issue, remember that winform controls
are STA (single thread apartment) model and therefore you have to marshall
calls back to the UI thread to make an update to the UI. Check out the
following links, you have to check a property on the control called
'InvokeRequired' and then use 'BeginInvoke' with a delegate to get the
update done on the UI thread.

http://www.theserverside.net/discussions/thread.tss?thread_id=25063

http://msdn.microsoft.com/library/d...mWindowsFormsControlClassBeginInvokeTopic.asp


HTH

Ollie Riches
 
J

Jon Skeet [C# MVP]

Eps said:
ok i have a timer object in my windows forms application, the app also
makes use of a COM object which also uses threading.

I have a seperate class that deals with COM object but this class has to
pass infomation back to the main windows form from time to time, so i
pass the main form to the class that that calls the COM object so i can
access the windows form GUI.

However this always causes errors...

A first chance exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

A first chance exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll

in the debugging window and the GUI never updates properly. I am fairly
certain it is to do with me calling the main form from the class that
deals with the COM object, probably a threading issue i think.

Anyone know how i can update my GUI from the seperate class ?

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml for
threading in Windows Forms - but quite how it should all work with COM,
I'm afraid I don't know. I've always tried to avoid COM as much as
possible :)
 
O

Ollie Riches

that would be 'yoda' as in small, green and talking in tongues then Jon.....

Ollie
 
J

Jon Skeet [C# MVP]

Ollie Riches said:
that would be 'yoda' as in small, green and talking in tongues then Jon.....

It would be "yoda" as in the name I used for the computer I built out
of bits and pieces while I was at university. It's the only computer
I've ever seen which not only *contained* a chopstick, but actually
*needed* it (in order to keep the ribbon connectors away from the
memory).

See http://www.pobox.com/~skeet/pics/yoda2.jpg for a piccie.
 

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