Threading & Events

E

elziko

I have an object (Object1) that instantiates another class (Object2) in a
seperate thread. It must be in its own thread for an ActiveX control it
hosts to work.

From Object1 I can get to all the methods of the ActiveX control in Object2
which is fine. However, I need to get some information back from Object2 as
its state changes. How would I go about informing Object1 of events that
occur in the ActiveX control?

At the moment I'm rtying a workaround where I use a System.Timers.Timer to
periodically check the properties of the ActiveX control I'm interested in.
However the timer mechanism seems to be very unreliable. Sometimes the
'Elapsed event' doesnt occur at all, and sometimes it seems to occur in
quick succession before the code inside the 'Elapsed' event has finished
executing.

Help on either of these matters would be great but I think the first idea
will be better since as this project grows I may have to check too many
events in the timer.
 
T

Tom Spink

Hi, create an event in your Object2 class, and attach a handler to that
event in Object1:

Public Class Object2
Public Event MyStateChange As System.EventHandler
 
C

CJ Taylor

elziko said:
I have an object (Object1) that instantiates another class (Object2) in a
seperate thread. It must be in its own thread for an ActiveX control it
hosts to work.

From Object1 I can get to all the methods of the ActiveX control in Object2
which is fine. However, I need to get some information back from Object2 as
its state changes. How would I go about informing Object1 of events that
occur in the ActiveX control?

At the moment I'm rtying a workaround where I use a System.Timers.Timer to
periodically check the properties of the ActiveX control I'm interested
in.

No.... don't do that...
However the timer mechanism seems to be very unreliable. Sometimes the
'Elapsed event' doesnt occur at all, and sometimes it seems to occur in
quick succession before the code inside the 'Elapsed' event has finished
executing.

Help on either of these matters would be great but I think the first idea
will be better since as this project grows I may have to check too many
events in the timer.

You want to look into Asyncronous programming. There is a great great deal
of information on it at devx.com if you interested. But basically, you want
this separate thread to process, and raise an event saying, "I did
something" from the active X controls. now, you "could" directly link the
event to a method in your first thread using a delegate (because of the
separate threads you need to have a callback method, create yet another
delegate, it gets confusing, I'll help you more when you get there)

Or you could get a bit more advanced and use AsyncCallbacks... It doesn't
really matter in my opinion because they are essentially the same thing.
Raise event when I'm done, tell someone else. Async Callbacks are just more
often used in Multithreaded situations.

But never use timers to "poll" data. This can lead to all sorts of problem
(for example if your trying to SyncLock or Mutex a variable on a callback,
the timer just messes it up, because its still running... So data could get
corrupted if your process takes longer than your timer interval, see where
I'm going?) And many other problems can occur. =)

Hope it helps and let me know if you have more questions.

CJ
 

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