C#中的并行事件处理 [英] Parallel event handling in C#

查看:23
本文介绍了C#中的并行事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个模块,它必须处理来自外部系统的许多事件.我必须使用第三方类提供一个事件 (OnNewMessage),将一些参数作为输入传递,两个作为输出传递,每个事件都需要大量时间才能被处理.我想以并行方式处理这些事件,以避免阻塞调用者并并行处理多个请求.

I’m developing a module that has to handle many events coming from an external system. I’ve to use a third party class providing an event (OnNewMessage) passing some parameters as input and two as output, each event require a bunch of time in order to be processed. I’d like to serve these events in a parallel way, in order to avoid blocking the caller and to process multiple request in parallel.

这是我的代码示例:

void Init()
{
   provider.OnNewMessage += new OnMessageEventHandler(processEvent);
}

void processEvent(string xml, int …, out string resultXML, out string description)
{
   ...
}

在 C# 3.5 中执行此操作的最佳方法是什么?

Which is the best approach to do this in C# 3.5?

谢谢

推荐答案

我会使用队列来存储事件,然后从线程池中的一组线程中使用该队列.

I would use a queue to store the events, and then consume that queue from a bounch of thread from the thread pool.

这篇关于C#中的并行事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆