Groups | Blog | Home
all groups > c# > september 2004 >

c# : Event from other thread


Dirk Reske
9/30/2004 11:13:43 PM
Hello,

I have a class, which starts a new thread.
from this thread events can be fired.
How can i fire this event in the class-creator-thread?


thx

Aidy
10/1/2004 2:15:29 AM
Read up on Delegates. You can have the creater pass a Delegate to your
thread and have the thread call the delegate when it wants to notify the
creater.

[quoted text, click to view]

Jon Skeet [C# MVP]
10/1/2004 8:05:33 AM
[quoted text, click to view]

That's only going to help if the creator thread is a UI thread, or
something else which implements ISynchronizeInvoke. Just calling a
delegate created by the creator thread isn't going to make it execute
in that thread.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
10/1/2004 8:39:10 AM
[quoted text, click to view]

Your creator thread will need to be running some kind of message pump,
whether your own or the normal Windows one. If your creator thread is a
UI thread, you can use Control.Invoke/Control.BeginInvoke. Otherwise,
you might want to use a producer/consumer type queue, with the creator
thread waiting on the queue and the other thread putting work items on
the queue.

See http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml for an
example of a producer/consumer queue.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button