如何将事件传递给线程 [英] How do I pass an event to a thread

查看:93
本文介绍了如何将事件传递给线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个股票dataFeed,该事件每秒触发大约4或5次事件,通过事件arg发送股票数据.我解析事件arg,然后通过写入的数据库对象将结果插入数据库.所有这些活动似乎使Windows窗体不堪重负,并消耗了大部分系统资源.我想将stock dataFeed事件放在一个线程上,但我不知道如何.我尝试创建ThreadStart对象,并使用匿名委托将事件传递给,但是我不知道如何实例化事件arg或要传递的对象...

例如:

Hi,

I have a stock dataFeed that has an event that fires approx 4 or 5 times per sec sending the stock data via a event arg. I parse the event arg and insert the results into a database via a database object that wrote. All of this activity seems to overwhelm the windows form and consumes most of system resources. I''d like to put the stock dataFeed event on a thread but I don''t know how. I''ve tried creating a ThreadStart object and using an anonymous delegate to pass the event to but I dont know how to instantiate the event arg or what object to pass...

example:

// dataFeed event
private void stockDataFeed(object sender, stockDataFeedArg arg)
{
   myDatabaseOject.Insert(arg.stockPrice);
}

private void Button_Click(object sender, EventArgs e)
{
   ThreadStart tS = delegate() {stockDataFeed(?,?);};
   Thread t = new Thread(tS);
   t.Start();
}



能否有人请向我解释/告诉我我做错了什么以及解决该问题的正确方法是什么.

谢谢!

-Donald



Can some one please explain/show me what I''m doing wrong and what''s the correct way to go about this.

Thanks!

-Donald

推荐答案

您不会在每个事件上都创建一个新线程.创建线程是一个非常昂贵的过程.

相反,我可能会考虑将数据添加到事件处理程序中的队列中.您从代码的开头而不是事件处理程序中开始的第二个线程监视此队列中要处理的项目并将其添加到数据库中.完成项目处理后,它会入睡x秒钟,然后醒来并再次检查队列.
You don''t create a new thread on every event. Creating a thread is a very expensive process.

Instead, I''d probably look into adding the data to a queue in your event handler. A second thread, that you start at the beginning of your code, NOT in the event handler, watches this queue for items to process and add to the database. When it''s done processing items, it falls asleep for x number of seconds before waking up and checking the queue again.


唐纳德,这可能会让您感到惊讶,但是对此的确切答案问题包含在我先前对您问题的第二个答复中.

从提示/技巧"部分的文章中阅读最后的代码示例所需要的一切:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

另请参阅本文的替代技巧和相关的Microsoft示例(仅适用于v.4.0,尽管我提供了适用于所有Framework 2.0版和更高版本的解决方案).
最后一个示例显示如何组织线程间调用.您可以使用此代码进行很少的更改:只需使用事件参数类型实例化泛型即可.

欢迎进一步的问题.

祝你好运!
—SA
Donald, it might surprise you, but the precise answer to this question is contained in my second answer to you previous question.

All you need it to read the last code sample from my Article from Tips/Tricks section:

Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

See also Alternative Tip to my article and related Microsoft sample (v.4.0 only, while I provide a solution good for all Framework versions 2.0 and later).
This last sample shows how to organize inter-thread invocation. You can use this code with very minimal changes: just instantiate generic with your event argument type.

Further questions are welcome.

Good luck!
—SA


这篇关于如何将事件传递给线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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