WaitForSingleObject usage, waitforsingleobject

Source: Internet
Author: User

WaitForSingleObject usage, waitforsingleobject
Corresponding function editing VC declaration DWORD WaitForSingleObject (HANDLE hHandle, DWORD dwMilliseconds); parameter editingHHandle[In] object handle. You can specify a series of objects, such as Event, Job, Memory resource notification, Mutex, Process, Semaphore, Thread, and Waitable timer.DwMilliseconds[In] timing interval, in milliseconds (MS). If a non-zero value is specified, the function is in the waiting stateHHandleThe marked object is triggered or the time has elapsed. IfDwMillisecondsIf the value is 0, the object is not triggered, and the function does not enter a waiting state. It always returns immediately. IfDwMillisecondsIf it is INFINITE, the function will return only after the object is triggered. The WaitForSingleObject function is used to detect the signal status of the hHandle event. When this function is called in a thread, the thread temporarily suspends. If the suspended dwMilliseconds is within milliseconds, if the waiting object of the thread changes to a signal state, the function returns immediately. If the timeout time has reached dwMilliseconds milliseconds, but the object indicated by hHandle has not become a signal state, the function returns the same result. The dwMilliseconds parameter has two special values: 0 and INFINITE. If the value is 0, the function returns immediately. If the value is INFINITE, the thread is suspended until the object indicated by hHandle changes to a signal state.WAIT_ABANDONED 0x00000080:When the hHandle is mutex, if the thread with mutex does not release the core object at the end, the returned value is returned. WAIT_OBJECT_0 0x00000000: the specified object has a signal status WAIT_TIMEOUT 0x00000102: Waiting For timeout WAIT_FAILED 0 xFFFFFFFF: error. You can get the error code through GetLastError. Here is an example: first, create a global Event object g_event: CEvent g_event. in the program, you can call CEvent: SetEvent to set the Event to a signal state. The following is a thread function MyThreadProc () UINT CFlushDlg: MyThreadProc (LPVOID pParam) {WaitForSingleObject (g_event, INFINITE); (;;){.............} Return 0;} In this thread function, the following for loop is executed only when g_event is set to a signal state. Because g_event is a global variable, we can use g_event in other threads. setEvent controls this thread. Another usage is that we can use the WaitForSingleObject function to execute a thread function at intervals. The UINT CFlushDlg: MyThreadProc (LPVOID pParam) {while (WaitForSingleObject (g_event, MT_INTERVAL )! = WAIT_OBJECT_0 ){..................} Return 0;} In this thread function, you can set MT_INTERVAL to control how often the function body of this thread is executed. When the event is in a non-signal state, the function is executed once every MT_INTERVAL, when the event is set to a signal state, the thread completes execution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.