LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can not terminate a thread when using SDK

I have code a program which will communicate between 2 process . At the server side , the code phase is as follows :


Static int quitFlag =1;
int main ()
{
int eThreadPoolHandle ;
HANDLE pipeHandle ;
int wThreadID ;
�.
CmtNewThreadPool (EGN_THREAD_NUM, &eThreadPoolHandle); //Create a thread pool
//Create a named pipe
pipeHandle=CreateNamedPipe(DATA_PIPE,PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT, AVAILABLE_PIPE_NUM,0, 0, INFINITE,NULL);
//Spawn a thread to monitor the connect from client side
CmtScheduleThreadPoolFunction (eThreadPoolHandle, WaitForConnectAsk, pipeHandle, &wThreadID);
�..
//When received the quit message , then do follows
CmtDiscardThreadPool (eThreadPoolHandle);

��
Ret
urn 0 ;
}


//The thread using to monitor the client�s call

int CVICALLBACK WaitForConnectAsk (void *functionData)
{
HANDLE pipeHandle ;

pipeHandle = functionData ;
while (quitFlag)
{
if(ConnectNamedPipe(pipeHandle,NULL))// Once there is a connect call , then execute the task
{
/* doing something */
���.
DisconnectNamedPipe(pipeHandle);
}
Sleep(300);
}

CloseHandle(pipeHandle);
return 0;
}

The problem is when execute the sentence "ConnectNamedPipe(pipeHandle,NULL)" , it will always wait for the client call , any belows sentence will not be executed . So when press the Quit button on the panel , I need finish the thread first but it seems not has any method to perform the task . Anyone can advise me how to terminate the thread ? Thanks.

David
0 Kudos
Message 1 of 7
(3,771 Views)
David,

The trick here is not in the ConnectNamedPipe;to be able to execute the ConnectNamedPipe wihtout blocking you execution you must specify this when you create the pipe.

When you call CreateNamedPipe you are specifying in the dwPipeMode parameter PIPE_WAIT; this causes the call to ConnectNamedPipe to hang until there is a connection thus not allowing you to terminate your application prperly.

Change that flag to PIPE_NOWAIT and you while loop should be running constantly, you may want to add a small delay there too.

I hope this helps.

Juan Carlos
N.I.
0 Kudos
Message 2 of 7
(3,771 Views)
Hello Juan,

yes ,you are right. but in this situation , the ConnectNamedPipe() always return FALSE, because my condition is this : if(ConnectNamedPipe(...)){ MessageBox(...);...}. So when the client using CreateFile(...) to construct a connect , I can not find the dialog window popup by the MesageBox(). I am not sure if it is due to the competion .

David
0 Kudos
Message 3 of 7
(3,771 Views)
Hi David,

The messagebox seems to only open if there is a valid connection to the pipe, as per your code. So, that may be why you are not seeing a messagebox appear. You would probably have to do some time of polling loop to run ConnectNamedPipe, and until there is a valid connection or the user presses the quit button will you exit that loop and finish out the function.

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 4 of 7
(3,771 Views)
Hi Jeremy,

I am not clear what you said , could you show a code phase ? thanks.

David Lee
0 Kudos
Message 5 of 7
(3,771 Views)
I meant something like this:

while (quitFlag)
{
int status;
do{
status = ConnectNamedPipe(pipeHandle,NULL));
} while (status == NOT_CONNECTED || quitFlag);
if (status == CONNECTED)
{
/* do your work */
}
}

Basically, add another loop to wait for a connection or a stop sign. That way, regardless of if you get the connection or not, if you press 'Stop', it'll quit out of your loops.

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 6 of 7
(3,771 Views)
Thanks Jeremy, I'll try it .

David
0 Kudos
Message 7 of 7
(3,771 Views)