LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

RegisterTCPServerEx into thread

Hello,

I want use the RegisterTCPServerEx into a Windows thread, my code is like the following:

 

static DWORD WINAPI RunThread(void* pCallbackData)

{

    int bExitThread = FALSE;


    // get TcpAcceptor pointer
    pTcpAcceptor = (PTR_TCP_ACCEPTOR)pCallbackData;

    RegisterTCPServerEx(pTcpAcceptor->iIpPort,
                        OnTcpEvent, //my callback tcp function
                        (void*)pTcpAcceptor,
                        pTcpAcceptor->sIpAddress);
    
    while(!bExitThread)
    {                            
        dwRes = WaitForSingleObject(pTcpAcceptor->hThreadExitEvent, 0);
    
        switch(dwRes)
        {
            case WAIT_OBJECT_0:
                // exit thread
                bExitThread = TRUE;
            break;
            case WAIT_TIMEOUT:
                // check for tcp client message
                ProcessSystemEvents();            
            break;
            default:
                //ERROR
                bExitThread = TRUE;
            break;
        }
    }
    
    // unregister server
    UnregisterTCPServerEx(pTcpAcceptor->iIpPort, pTcpAcceptor->sIpAddress);
}

static int CVICALLBACK OnTcpEvent(unsigned int uiClientId, int iEvent,
                                  int iError, void* callbackData)
{    
    PTR_TCP_ACCEPTOR pTcpAcceptor = (PTR_TCP_ACCEPTOR)callbackData;
    
    switch(iEvent)
       {
        case TCP_CONNECT:
            ......
        break;
          case TCP_DATAREADY:
              ....
        break;
           case TCP_DISCONNECT:
            ....
           break;
    }
    
    return 0;
}

 

This code works, but sometimes the callback function "OnTcpEvent", is not called anymore. It seems that the ProcessSystemEvents

is "blocked". In fact, inserting a breakpoint on the line corresponding to the call to the ProcessSystemEvents function, 

the process never stops here. The thread is still active, each new connection made by a client is accepted but the callback

OnTcpEvent is never called.

 

Could you please tell me how to properly use the RegisterTCPServerEx function in a thead?

 

Thanks for your help.

 

Best regards.

Patrizio

 

 

0 Kudos
Message 1 of 3
(2,924 Views)

Hi Patrizio,

 

is the ProcessSystemEvents returning something before you call it another time?

I'm asking you that because since this function allows other callback functions to execute before it completes, maybe it seems it has finished his task, but it's still in execution and any other calls start the troubles.

 

Hope it helps,

have a nice day.

 

FBM

0 Kudos
Message 2 of 3
(2,900 Views)

I put a trace of the ProcessSystemEvents return, and I will retry a test on my code.

I also will check the callback functions that probably ProcessSystemEvents will call, to look for any deadlock.

 

Thank you for your reply!

 

Patrizio

 

0 Kudos
Message 3 of 3
(2,894 Views)