如何在线程中使用事件 [英] how to use event in threads
本文介绍了如何在线程中使用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何在线程概念中使用事件..
我指的是msdn帮助..但是程序很难理解..
你能帮我吗如何执行该过程...
i don''t know how to use event in the threading concept..
i refer the msdn help.. but the program is very difficult to understand..
so can u help me. how to do that process...
推荐答案
好像您想了解很多有关内核对象的内容:).好吧,基本上是这样的:
Looks like you want to know a lot about kernel objects :). Well, basically it''s this way:
- 通常通过调用 CreateEvent 函数
- 来创建事件,然后将此事件的句柄传递给将使用该事件的所有线程.根据情况,有很多方法可以做到这一点-从在线程函数参数中传递句柄到在另一个进程中复制它
- 当任何线程需要等待此事件发生时,您调用 WaitForSingleObject 或 WaitForMultipleObjects 函数,将事件句柄作为参数传递
- 当满足等待条件(例如,您要处理的数据)时,另一个线程通过调用 SetEvent 函数
- 引发事件,下次等待线程接收处理器时间时, WaitForSingle 对象返回,因此该线程可以继续执行
- 在需要时,可以通过 ResetEvent 函数重置事件(处于尚未发生"状态),因此您可以从步骤3开始重复该过程.
- 需要时,应通过调用 CloseHandle 方法
- You create an event, usually by calling CreateEvent function
- You pass handle of this event to all the threads that shall use your event. Depending on situation, there are numerous ways to do that - from passing handle in thread function arguments to duplicating it in another process
- When any of your threads need to wait for this event to occur, you call WaitForSingleObject or WaitForMultipleObjects function, passing event handle as an argument
- When condition being wait for is satisfied (you get the data to process, for example), another thread raises event by calling SetEvent function
- Next time waiting thread receives processor time, WaitForSingle object returns, so this thread can continue execution
- When needed, event can be reset (put in "not yet occured" state) by ResetEvent function, so you can repeat the process from step 3
- When no mere needed, Event handles should be closed by calling CloseHandle methods
关闭事件句柄
这是非常简短且不完整的描述,缺少许多细节,但它应该为您提供基础知识.在更深入地研究MSDN之前要先做一些事情:)
This is very brief and incomplete description, missing a lot of details, but it should give you the basics. Something to start with before diving deeper into MSDN :)
这篇关于如何在线程中使用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文