Catching events in separate thread?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I have a windows form that uses an imported COM class to talk to a legacy
system. The COM object works asynchronously - An even is fired when data has
been returned.

This works fine, except that Im getting too many events fired! Thus my
windows form becomes pretty sluggish and unresponsive, and CPU utilistaion
is pretty high.

Is there any way I can set up another thread to recieve and handle these
events instead? Do threads have event loops? If so, how do I "direct" events
to them?

Am I tackling this in the wrong way?

Thanks!

Spammy
 
You said that when the data is returned asynchronously, an event is raised, right? So you must have some handy code written within that event handler. this amount of code is a candidate for running in a separate worker thread. Place the event handling code in a new private method and register it with the ThreadStart delegate, andfollow up by spawning a thread using the ThreadStart delegate. When spawned, this worker thread should run the code and exit at the end of execution. The The ThreadStart delegate will only run a method that takes no parameters...so you need to think about how to pass information to the method.Remember, a method in a class has access to member variables, regardless of which thread it runs on. If your code is bound to changes some instance member values , then makes sure the part of the code is rendered thread-safe. Read more about Threading on your local MSDN copy. Its not all that difficult as it is made out to be.
 
Jaison John said:
You said that when the data is returned asynchronously, an event is
raised, right? So you must have some handy code written within that event
handler. this amount of code is a candidate for running in a separate worker
thread. Place the event handling code in a new private method and register
it with the ThreadStart delegate, andfollow up by spawning a thread using
the ThreadStart delegate. When spawned, this worker thread should run the
code and exit at the end of execution. The The ThreadStart delegate will
only run a method that takes no parameters...so you need to think about how
to pass information to the method.Remember, a method in a class has access
to member variables, regardless of which thread it runs on. If your code is
bound to changes some instance member values , then makes sure the part of
the code is rendered thread-safe. Read more about Threading on your local
MSDN copy. Its not all that difficult as it is made out to be.

Hi Jaison,

I have in fact considered this - the thing is that the code in the event
handler is pretty trivial - its just updating some fields in an array. I
suspect that moving this to a thread would be counterproductive, as creating
a thread each event would be the most resource heavy action per event. In
any case, the main even loop would still be hit 10-20 times per second,
before it starts the thread.

How about this - create a thread ONCE, at the start, to handle the event
work? In this case, how would I a) keep this "worker" thread in a "waiting"
state, and b) send a message to it to tell it to wake up and do some work?

Spammy
 
How about this - create a thread ONCE, at the start, to handle the event
work? In this case, how would I a) keep this "worker" thread in a "waiting"
state, and b) send a message to it to tell it to wake up and do some work?

Definately the way to go in my opinion. Read up on "Thread Pooling" on MSDN
for good info on how to do this.

AndyC
 
Back
Top