12-26-2010 09:50 PM
Hello,
Using the Windows' named pipe to communicate between the two applications, the main function code phase is like this:
static HANDLE pipe_rcvDB; /*pipe handle*/
static HANDLE hPipeEvent;/*pipe event handle*/
int main()
{
...;
pipe_rcvDB = CreateNamedPipe(DB2HMI_PIPE, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,
1,1024, 1024, 1000, NULL);
hPipeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);/*Create the overlapped I/O event*/
CmtNewThreadPool(2, &thdDBServerHandle);
/*Spawn the thread to listen the message from the pipe*/
thdStatus = CmtScheduleThreadPoolFunctionAdv(thdDBServerHandle, MonitorDBServerData, NULL, THREAD_PRIORITY_NORMAL, NULL,
EVENT_TP_THREAD_FUNCTION_BEGIN, NULL, station_thdID = CmtGetCurrentThreadID(), &pipeMonitorTHD_ID);
...
}
/*The pipe msg listen thread*/
int CVICALLBACK MonitorDBServerData(void *functionData)
{
DWORD nReadByte = 0;
DWORD dwByte = 0;
OVERLAPPED OverlapStruct;
size_t status;
memset(&OverlapStruct, 0, sizeof(OVERLAPPED));
OverlapStruct.hEvent = hPipeEvent;
while (TRUE)
{
ProcessSystemEvents();
ConnectNamedPipe(pipe_rcvDB, &OverlapStruct);
WaitForSingleObject(hPipeEvent, INFINITE);
/*check if there is a signal to finish the thread*/
if (!GetOverlappedResult(pipe_rcvDB, &OverlapStruct, &dwByte, TRUE))
{
break;
}
...;
}/*end of while loop*/
}
/*The call back of quit the application */
int CVICALLBACK QuitApplication(int panelHandle, int controlID, int event, void *callbackData, int eventData1, int eventData2)
{
if (event == EVENT_COMMIT)
{
SetEvent(hPipeEvent); /*Trigger the overlapped to stop the listen thread*/
CmtWaitForThreadPoolFunctionCompletion(thdDBServerHandle, pipeMonitorTHD_ID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CloseHandle(pipe_rcvDB);
CmtDiscardTSQ(tsqHandle);
CmtDiscardThreadPool(thdDBServerHandle);
DiscardPanel(dataEditPanel);
}
}
When pressing the quit button, and by using the Windows API, the SetEvent() to trigger a signal for the thread, the thread can not received the signal, I hope the function GetOverlappedResult() in the thread can receive the signal, but it has no any response. Does CVI support the API?
David
12-27-2010 07:50 PM
Hi David,
Yes, LabWindows/CVI does support this API. You can search LabWindows/CVI Help for detailed information. Hope this helps.
Xiaojian Wang
12-27-2010 09:14 PM
Hi,
Um, but why the SetEvent() can not trigger the GetOverlappedResult() and make a terminating event.
David