Re: Catching events in separate thread? by spammy
spammy
Fri Jun 04 02:57:15 CDT 2004
"Jaison John" <anonymous@discussions.microsoft.com> wrote in message
news:1707E5CB-2D5B-45DF-9073-EB2DE936384B@microsoft.com...
> 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