如何在单独的线程中触发事件处理程序?
我创建了一个服务,它将为列表中的每个项目触发一个事件。 我使用了 foreach 循环来迭代列表并触发列表中每个项目的事件。 为了提高服务性能, 有人建议我尝试在单独的线程中运行事件处理程序。 我对线程的概念很陌生, 我可以获得一些关于如何在单独的线程上仅运行事件处理程序的建议吗?我应该为列表中的每个项目创建一个单独的线程吗?
欢迎任何建议。
这是我的代码:
public void UpdateMessages(){
foreach(var message in messages)
{
message.Received +=
TextMessage_MessageReceived;
}
}
private void RawMessage_MessageReceived(object sender, MessageEventArgs<TextMessage> e)
{
var msgText = sender as RealtimeTextMessage;
RawMessageList[msgText].SetMessageBytes(msgText);
}
在代码片段中,我有事件处理程序 RawMessage_MessageReceived ,它将执行从原始消息中提取字节的操作。 我需要在不同的线程中执行事件处理,因为根据我对线程的理解,单个线程将负责引发每个消息的事件,并同时负责每个事件的事件处理。 如何编写一个单独的线程来处理事件处理部分?
I've created a service which will fire an event for each of the item in the list.
I've used a foreach loop to iterate through the list and trigger the event for each of the item in the list.
In order to improve the performance of the service,
It was suggested that i try to run the event handler in a separate thread.
I'm new to the concept of threading,
Can I get some suggestions on how I can run only the event handler on a separate thread? Should I create a separate thread for the each of the item in my list?
Any suggestions are welcome.
Here is my code:
public void UpdateMessages(){
foreach(var message in messages)
{
message.Received +=
TextMessage_MessageReceived;
}
}
private void RawMessage_MessageReceived(object sender, MessageEventArgs<TextMessage> e)
{
var msgText = sender as RealtimeTextMessage;
RawMessageList[msgText].SetMessageBytes(msgText);
}
In the code snippet I have the event handler RawMessage_MessageReceived which will perform the operation of extracting the bytes from the raw message.
I need to perform the event handling in a different thread since from what I understand about threading, a single thread will be responsible for raising the event for each message and simultaneously will be responsible for event handling of each event.
How can I write a separate thread to take care of the event handling part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论