Reprint: Win32 cevent Event

Source: Internet
Author: User

An object of the CEvent class that represents an "event (a synchronization object that allows a thread to notify another thread when one event occurs)".

Events are useful when a thread needs to know when to perform a task. For example, when copying data to a data document, the line thread executes is notified when the data is available. When new data is available, the thread can execute as soon as possible by using the CEvent object to notify the copy thread.

For example, in some network applications, a thread (recorded as a) is responsible for listening to the communication port, and the other thread (recorded as B) is responsible for updating the user data. By using the CEvent class, thread A can tell thread B when to update user data so that thread B can update user data as soon as possible.


There are two types of CEvent objects: Automatic and Manual.

1) A manual CEvent object exists in the state set by resetevent or SetEvent until another function is called.

2) An automatic CEvent An object automatically returns an unmarked (useless) state after at least one thread has been freed.

To use a CEvent object, you should construct a CEvent object when needed. Specifies the event to wait on, and the app should have it to access the event when the constructor returns. Call the SetEvent tag (make available) event object, and then call the Unlock function When you are finished accessing the control resource.
Another way to use the CEvent object is to add a variable of type CEvent to make it a data member of the class you want to control. During a control object being constructed, you can call the constructor of the CEvent data member, which indicates whether the time is initially flagged, the type of event object required, the event name (if it is to be used in the process), and the security attributes you want.

The constructor prototypes for the CEvent class are as follows:

CEvent (
BOOL Binitiallyown/ * = FALSE * / ,                                  //used to specify whether the initial state of the event object is a sending state (default is no letter) 
BOOL bManualReset/ * = FALSE * / ,                                //used to specify whether the created event object is an automatic event or a manual event object (the default is an automatic event object) 
LPCTSTR LpszNAme/ * = NULL * / ,                                   //Used to define the name of the event object 
lpsecurity_attributes Lpsaattribute/ * = NULL * /      //Pointer to a lpsecurity_attributes structure 

Three methods provided by the CEvent class

SetEvent ()//Set event to send status and release other waiting threads
PulseEvent ()//Set the event to the sending State and release the other waiting threads, then set the event to the non-sending State
ResetEvent ()//Set event to not send status

1. Automatic Event Objects
If you use the default parameter value of the CEvent class constructor, the object defined is an automatic event object. The initial state is not a letter state, you can use SetEvent to make it into the sending State, waiting for the first thread in the thread to resume running, but the event object will automatically turn it into a non-sending state, so that other waiting-state threads are still blocked. That is, an automatic event object can only start one thread at a time in a wait state.

Example: An application that, when the user presses the left mouse button on a program window, creates and starts two threads that, after the two threads are started, each display an information box that indicates that the thread has been started and that the thread is suspended by the Lock function of the event object. When the user presses the right mouse button in the program window, the other thread is started, and the event object is set to the "Sent" state in the thread, thus initiating the first suspended thread.
1. Create a new single document program;
2. Define a global event object in the implementation file for the view class:

CEvent EVENTOBJ;

3. Write the following thread function in the implementation file of the view class:

UINT MessageThread1 (LPVOID pparam)
{
LPTSTR pmessage=_t ("Thread1 is started");
cwnd* Pmainwnd=afxgetmainwnd ();
:: MessageBox (pmainwnd->m_hwnd,pmessage,_t ("Thread message"), MB_OK);
Eventobj.lock ();//thread 1 is in a wait state
/*-----------------------------------------------------------------*/
pmessage=_t ("Thread1 is unblocked");
:: MessageBox (pmainwnd->m_hwnd,pmessage,_t ("Thread1 message"), MB_OK);//Display thread 1 after unlocking the message box
Eventobj.lock ();//thread 1 is in a wait state again
/*-----------------------------------------------------------------*/
pmessage=_t ("Thread1 is unblocked again");
:: MessageBox (pmainwnd->m_hwnd,pmessage,_t ("Thread1 message"), MB_OK);//Display thread 1 after unlocking the message box
return0;
}
UINT MessageThread2 (LPVOID pparam)
{
LPTSTR pmessage=_t ("Thread2 is started");
cwnd* Pmainwnd=afxgetmainwnd ();
:: MessageBox (pmainwnd->m_hwnd,pmessage,_t ("Thread message"), MB_OK);
Eventobj.lock ();//Thread 2 is in a wait state
/*-----------------------------------------------------------------*/
pmessage=_t ("Thread2 is unblocked");
:: MessageBox (pmainwnd->m_hwnd,pmessage,_t ("Thread2 message"), MB_OK);//Display thread 2 after unlocking the message box
return0;
}
UINT MessageThread3 (LPVOID pparam)
{
Eventobj.setevent ();//Set the event object to the sending State
return0;
}

4. The mouse response message for the view class is as follows:

voidCthreadtestview::onlbuttondown (UINT nflags, CPoint Point)
{
AfxBeginThread (MessageThread1, _t ("Thread is started"));//Start thread 1
AfxBeginThread (MessageThread2, _t ("Thread is started"));//Start thread 2
Cview::onlbuttondown (nflags, point);
}

voidCthreadtestview::onrbuttondown (UINT nflags, CPoint Point)
{
AfxBeginThread (MessageThread3, _t ("Thread is unblocked"));//Start thread 3
Cview::onrbuttondown (nflags, point);
}

Program Run Result:

2. Manual Event Object
Once the manual event object is set to "send" state with function SetEvent , it is always in a valid state, unless you use the object's member function PulseEvent or resetevent to reset it to "not sent State So the manual event object is used to recover multiple running threads in the waiting state.

Example: Define the event object for the above example as a manual event object, and then run the program.
Change to the following code:

//Change the code that defines the event object to
CEvent EVENTOBJ (false,true);

Program Run Result:

Resources:
1.CEvent Type: http://www.360doc.com/content/10/0411/04/896768_22478483.shtml
2.CEvent Encyclopedia: http://baike.baidu.com/view/2894213.htm
3. Event (CEvent)--mfc:http://blog.sina.com.cn/s/blog_627ebfc30100iu7k.html

Source: http://blog.csdn.net/akof1314/article/details/5767740

Reprint: Win32 cevent Event

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.