LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

stop dll execution LabWindows/CVI

Dear all,I'm new to Labwindows/CVI and I'm not experienced with C...

My problem is very simple: I have to produce a system that calls functions contained into a DLL but I have to produce a mechanism to halt the execution of the called function.

In other word I call a DLL function and, if the user request it (for example if it takes too time), I have to terminate the execution of this function.

 

The real scope is:

 

LabVIEW calls a wrapper DLL.

The wrapper DLL calls (by name, contained into a string argument) a function contained into another DLL (defined by the user)

LabVIEW should terminate the execution of the previous step.

 

I have tried to stop directly the LabVIEW thread using the "abort VI", but I found that it doesn't work due LV is locked (or better the VI that calls a DLL function is locked) until the DLL function execution has been finished.

 

So I'm thinking to use threads in LabWindows/CVI to execute the DLL function (I will start one thread for each function) and the to use the CmtTerminateThreadPoolThread function of LabWindows/CVI to stop the execution of a thred.

 

I can't use the CmtExitThreadPoolThread function because my thread can't call the ProcessSystemEvents function because it will just call a DLL function, the thread code is similar to:

 

int CVICALLBACK executeExternalFunction(void* functionData) {

     externalFunctionArray[externalFunctionToExecuteArrayIndex]((char*) functionData);

}

 

Does the CmtTerminateThreadPoolThread function makes the thread to release all its resources (it will open GPIB handles)?

There is another way to stop a thread execution?

 

thanks

Andrea Costantino 

0 Kudos
Message 1 of 3
(3,734 Views)

Hi Milaus,

 

I want to suggest you this links:

 

http://zone.ni.com/reference/en-XX/help/370051M-01/cvi/libref/cvicmtterminatethreadpoolthread/

 

http://forums.ni.com/ni/board/message?board.id=180&requireLogin=False&thread.id=14409

 

http://forums.ni.com/ni/board/message?board.id=180&requireLogin=False&thread.id=18528

 

After that you can create 2 parallel while loop? If the first is calling the dll and becoming blocked you may kill it from the second one if a timeout occoure.

 

Hope this help,

 

M

Matteo Brunella
Application Engineer
NI Italy
0 Kudos
Message 2 of 3
(3,716 Views)

Thanks Matteo,

 

but I would like to have more details about these solutions:

 

I read the CmtTerminateThreadPoolThread function terminate immediadly the thread execution. My thread will call one DLL function that will open a VISA session to an instrument and will produce a measure (that could be a complex measure). So my thread call just one function, as you can see in the code I have attached (function executeExternalFunction). What happen to the VISA session opened by the external DLL? I suppose that the OS won't release the resource due the external DLL will still be in memory (loaded by my wrapper) and any function has called a close session method...

 

So my C thread can't listen any OS message, due it is locked by the execution of the external function.

If I move the problem to LabVIEW level, I found that the VI can't be interrupted until it doesn't terminate the execution of the DLL function called (in other words the "abort vi" method doesn't work.

 

Just for completeness I explain the scope of the problem.

We have to produce a wrapper that allow to call C functions using a TCP/IP. We won't develop the C functions but we have to develop a framework that allow to the user to develop hisown functions and to call them using a TCP/IP client.

My idea is to develop a DLL that offers a method to register function, the user can include this DLL into his one and can register his function into an array of function pointers. Then my LabVIEW application will implement the TCP/IP protocol and will call the registerd functions.

 

Hereafter an example of how my DLL werapper will be implemented.

 

// Types
            typedef int (*ExternalFunctionPointer)(char *arg);

// Global variables
            int functionID = -1;
            ExternalFunctionPointer externalFunctionArray [2];

// Global functions

            int externalFunction1(char * argument) {
                        while (1) {
                                    Delay(1);
                                    printf("data of externalFunction1: ");
                                    printf("%s", argument);
                                    printf("\r\n");
                        }
            }

            int externalFunction2(char * argument) {
                        while (1) {
                                    Delay(1);
                                    printf("data of externalFunction2: ");
                                    printf("%s", argument);
                                    printf("\r\n");
                        }
            }

            int CVICALLBACK executeExternalFunction(void *functionData) {
                        externalFunctionArray[0]((char*)functionData);
                        return 0;
            }

            int runme() {
// start a new thread
                        printf("registering functions...\r\n");
                        externalFunctionArray[0]=externalFunction1;
                        externalFunctionArray[1]=externalFunction2;
                        printf("creating thread...\r\n");
                        CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE, executeExternalFunction, "argument", &functionID);
                        printf("waiting for execution...\r\n");
                        Delay(5);
                        CmtTerminateThreadPoolThread(DEFAULT_THREAD_POOL_HANDLE, functionID, 0);
                        printf("done!\r\n");
                        Delay(2);
                        return 0;
            }

            int main() {
                        runme();
                        return 0;
            }

0 Kudos
Message 3 of 3
(3,711 Views)