利用事件 控制程序运行
来源:互联网 发布:国泰君安炒股软件 编辑:程序博客网 时间:2024/05/21 06:25
This function creates a named or an unnamed event object.
HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPTSTR lpName );
- lpEventAttributes
- [in] Ignored. Must be NULL.
- bManualReset
- [in] Boolean that specifies whether a manual-reset or auto-reset event object is created. If TRUE, then you must use theResetEvent function to manually reset the state to nonsignaled. If FALSE, the system automatically resets the state to nonsignaled after a single waiting thread has been released.
- bInitialState
- [in] Boolean that specifies the initial state of the event object. If TRUE, the initial state is signaled; otherwise, it is nonsignaled.
- lpName
- [in] Pointer to a null-terminated string that specifies the name of the event object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.
If lpName matches the name of an existing named event object, the bManualReset and bInitialState parameters are ignored because they have already been set by the creation process.
If lpName is NULL, the event object is created without a name.
If lpName matches the name of an existing semaphore, mutex, or file-mapping object, the function fails and theGetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
A handle to the event object indicates success. If the named event object existed before the function call, the function returns a handle to the existing object andGetLastError returns ERROR_ALREADY_EXISTS. NULL indicates failure. To get extended error information, callGetLastError.
Remarks
The handle returned by CreateEvent has EVENT_ALL_ACCESS access to the new event object and can be used in any function that requires a handle to an event object.
Any thread of the calling process can specify the event-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one of the specified objects is signaled. When a wait function returns, the waiting thread is released to continue its execution.
The initial state of the event object is specified by the bInitialState parameter. Use theSetEvent function to set the state of an event object to signaled. Use theResetEvent function to reset the state of an event object to nonsignaled.
When the state of a manual-reset event object is signaled, it remains signaled until it is explicitly reset to nonsignaled by theResetEvent function. Any number of waiting threads, or threads that subsequently begin wait operations for the specified event object, can be released while the object's state is signaled.
When the state of an auto-reset event object is signaled, it remains signaled until a single waiting thread is released; the system then automatically resets the state to nonsignaled. If no threads are waiting, the event object's state remains signaled.
Multiple processes can have handles of the same event object, enabling use of the object for interprocess synchronization. The following object-sharing mechanism is available: a process can specify the name of an event object in a call to theCreateEvent function.
You can use the SetEventData function to associate data with an event handle.
Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The event object is destroyed when its last handle has been closed.
情景:
程序需要定时保存数据, 但是保存的过程中仍可能会响应IME输入法消息
目的:
程序在不保存数据时,才响应IME消息
方法: 可以采用事件的方法
首先创建一事件,有信号
保存数据前,设置无信号
保存数据后,设置为有信号
有信号时,才立即响应IME消息
程序:
HANDLE m_hSaveSign;
//第一个TRUE 表示: 人工复位 如果是FALSE 则是自动复位------WaitSingleObject后,会自动将事件复位为无信号
//第二个TRUE 表示: 事件有信号,
m_hSaveSign = CreateEvent(NULL,TRUE,TRUE,NULL);
//在OnTimer中操作事件
void CMainFrame::OnTimer(UINT nIDEvent){// TODO: 在此添加消息处理程序代码和/或调用默认值if(nIDEvent == m_autosaveID){//复位 无信号ResetEvent(m_hSaveSign);//自动保存AutoSavePrepage();//设置信号SetEvent(m_hSaveSign);}CFrameWnd::OnTimer(nIDEvent);}
//响应IME消息
// 无信号 说明正在保存数据
void CImportView::OnImeChar(wchar_t* Str){WaitForSingleObject(((CMainFrame*)AfxGetMainWnd())->m_hSaveSign,2000); //---- }
- 利用事件 控制程序运行
- 利用互斥控制程序只有一个运行实例
- 控制程序唯一运行
- 控制程序运行一次
- 控制程序运行一次
- 控制程序自动运行
- qt 利用qml编写控制程序
- 控制程序运行实例的个数
- 利用文件锁控制程序的执行
- 用VC++实现控制程序运行一实例
- VC控制程序只运行一次效果
- Windows开发--控制程序运行单一进程
- 控制程序运行实例个数——只运行一个实例
- 利用render事件来提高程序的运行效率。
- 利用render事件来提高as3程序的运行效率
- 在VC++实现控制程序只运行唯一一个实例
- VC的调试工作的解析--控制程序的运行
- Python:通过自定义系统级快捷键来控制程序运行
- 投资回报率的计算
- flex_flex开发的点点滴滴;
- android 应用签名
- java中split分割函数
- JAVA中SERIALVERSIONUID的作用
- 利用事件 控制程序运行
- 将win7电脑无线网变身WiFi热点,让手机、笔记本共享上网
- C#与VB.net混合开发测试,感受微软大软件平台思想
- C# 如何知道一个影像文件能够播放多长时间
- 强制加载 Hdpi资源 (2.3 dpi < 240)
- photography图片资源支持json格式,Ajax请求
- android UI 配置参数
- vsftpd安装教程
- wordpress 要用到的软件