2

我是 C# 中事件处理和线程的新手,所以如果这个问题是基本的,请原谅我:如何创建多个类以在不同的线程上运行,所有类都在监听同一个事件?

例如,我经常收到一条新数据,但时间间隔是随机的。当这个数据到达时,我用新数据更新一个类,我们称之为它MyDataClass,它会引发一个事件:MyDataClass.NewEvent

然后我有一个名为NewEventHandler. 当一个事件触发时,这个类对新数据进行一些计算,然后将结果发送到另一个应用程序。

所以问题是这样的:我需要大约 30 个实例来NewEventHandler收听MyDataClass.NewEvent(每个实例都进行不同的计算并产生不同的结果)。重要的是这些计算都同时运行 - 一旦事件触发所有 30 个NewEventHandler开始计算实例。但是,它们是否一起完成并不重要(例如,这里不需要同步)。

我如何实际创建实例,NewEventHandler以便它们在不同的线程上运行,并让它们都听一个实例MyDataClass.NewEvent

4

2 回答 2

2

在 C# 中,一般做法是在触发事件的同一线程上调用事件侦听器的方法。从源类触发事件的标准模板是:

void FireNewEvent() 
{
     var tmp = NewEvent;
     if( tmp != null )
     { 
         tmp(this, YourEventArgsInstance);
     }
}

事件不多,但有点荣耀的代表。所以这个调用类似于多播委托调用——这意味着所有订阅者将在FireNewEvent运行的同一线程上按顺序调用。我建议你不要改变这种行为。

如果要同时运行事件订阅者,则在每个订阅者中启动一个新任务。

...
MyDataClass.NewEvent += OneOfSubscriberClassInstance.OnNewEvent;

...
public void OnNewEvent(object sender, YourEventArgs args)
{ 
   Task.Factory.StartNew( () => {

       // all your event handling code here 
   });
}

触发事件的代码将依次触发 30 个订阅者,但每个订阅者将在 TPL 安排的自己的线程中运行。因此,触发事件的委托将不必等待触发下一个订阅者的处理程序,直到当前调用的订阅者的处理程序完成处理事件。

于 2013-06-05T18:26:27.817 回答
0

这是一个示例/演示如何同步不同的线程并确保它们都同时响应事件。您可以将此代码复制并粘贴到控制台应用程序中以查看它的运行情况。

public class Program
{
    private static EventWaitHandle _waitHandle;
    private const int ThreadCount = 20;
    private static int _signalledCount = 0;
    private static int _invokedCount = 0;
    private static int _eventCapturedCount = 0;
    private static CountdownEvent _startCounter;
    private static CountdownEvent _invokeCounter;
    private static CountdownEvent _eventCaptured;


    public static void Main(string[] args)
    {
        _waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
        _startCounter = new CountdownEvent(ThreadCount);
        _invokeCounter = new CountdownEvent(ThreadCount);
        _eventCaptured = new CountdownEvent(ThreadCount);

        //Start multiple threads that block until signalled
        for (int i = 1; i <= ThreadCount; i++)
        {
            var t = new Thread(new ParameterizedThreadStart(ThreadProc));
            t.Start(i);
        }

        //Allow all threads to start
        Thread.Sleep(100);

        _startCounter.Wait();

        Console.WriteLine("Press ENTER to allow waiting threads to proceed.");
        Console.ReadLine();

        //Signal threads to start
        _waitHandle.Set();

        //Wait for all threads to acknowledge start
        _invokeCounter.Wait();

        //Signal threads to proceed
        _waitHandle.Reset();

        Console.WriteLine("All threads ready. Raising event.");

        var me = new object();

        //Raise the event
        MyEvent(me, new EventArgs());

        //Wait for all threads to capture event
        _eventCaptured.Wait();
        Console.WriteLine("{0} of {1} threads responded to event.", _eventCapturedCount, ThreadCount);
        Console.ReadLine();
    }

    public static EventHandler MyEvent;

    public static void ThreadProc(object index)
    {
        //Signal main thread that this thread has started
        _startCounter.Signal();
        Interlocked.Increment(ref _signalledCount);

        //Subscribe to event
        MyEvent += delegate(object sender, EventArgs args) 
        { 
            Console.WriteLine("Thread {0} responded to event.", index);
            _eventCaptured.Signal();
            Interlocked.Increment(ref _eventCapturedCount);
        };

        Console.WriteLine("Thread {0} blocks.", index);

        //Wait for main thread to signal ok to start
        _waitHandle.WaitOne();

        //Signal main thread that this thread has been invoked
        _invokeCounter.Signal();
        Interlocked.Increment(ref _invokedCount);
    }
}
于 2013-06-05T23:02:11.633 回答