LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using Sleep() and asynchronous timer

Hello,

I am currently facing the problem that I need to read a value of a TCP/IP device with certain frequency (1Hz). When I set up an asynchronous timer with the desired frequency it reads the values in time. Now I want to use I CVICALLBACK function started of the UI containing the Sleep() function. While sleeping the async timer is not generating ticks. After sleeping it generates the missing ticks. Compared to the NI example "asyncdem.c" that I modified with a WaitFunction containing Sleep() [this is generating ticks while sleeping], it seems like the async timer in my program runs on the same thread as the thread executing Sleep(). I assume that async timer will not automatically use a new thread.

 

Is there a way to tell the async timer to use a new thread? Or is there a better way to solve this problem? Unfortunately I am a beginner in multi-threading.

 

Thanks in advance

Marius

0 Kudos
Message 1 of 6
(3,196 Views)

Asynchronous timers in CVI always run in a thread different from the main thread that usually handles the user interface, and this without need for you to do anything special. Given this, the reason why your program apparently freezes while performing a Sleep () may be elsewhere. How have you started the async timer? How long is your Sleep call? Can you post the relevant code?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(3,150 Views)

First of all thank you for your quick response. Here are the relevant code snippets: ( g_timerId is defined as static int)

 

// Connect / Disconnect (Setting async timer)
int CVICALLBACK connectCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){
int toggleButtonVal;

if(event == EVENT_COMMIT){
GetCtrlVal (panelHandle, PANEL_ConnectButton, &toggleButtonVal);

// Set wait curser
SetWaitCursor(1);

// connect
if(toggleButtonVal==1){


// Start timer
g_timerId = NewAsyncTimer(1.0, -4, 1, TimerCallback, NULL);
if (g_timerId <= 0){
MessagePopup("Async Timer", "Async timer could not be created");
g_timerId = 0;
return -1;
}
}
else{
// Stop timer
DiscardAsyncTimer (-1);
g_timerId = 0;
}

// Set wait curser
SetWaitCursor(0);

}
return 0;
}

 

// This function gets called when the timer is generating a tick (1s)
int CVICALLBACK TimerCallback (int reserved, int theTimerId, int event, void *callbackData, int eventData1, int eventData2){
char *status;

double currentTime;
double deltaTime;

if( (status=malloc(512*sizeof(float64)))==NULL ){
MessagePopup("Error","Error during allocating varaiables. Not enough resources available");
}

if (event == EVENT_TIMER_TICK ){
GetAsyncTimerAttribute (theTimerId, ASYNC_ATTR_TIMER_TIME, &currentTime);
GetAsyncTimerAttribute (theTimerId, ASYNC_ATTR_DELTA_TIME, &deltaTime);

 

// display the read string in UI
SetCtrlVal(panelHandle, PANEL_ServoMsg, status);

 

// debug purpose
printf("Running: %f\n",currentTime);
printf("ThreadID: %d\n",CmtGetCurrentThreadID ());

}
if( status ){
free(status);
status = NULL;
}
return 0;
}

 

// wait
int CVICALLBACK cWaitCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2){

 

if( event==EVENT_COMMIT){
Sleep(5000);

printf("WaitThreadID: %d\n",CmtGetCurrentThreadID ());
}
return 0;

}

 

The thread IDs of the wait thread and the async timer is different, but the timer isn't generating ticks while sleeping.

 

 

 

 

0 Kudos
Message 3 of 6
(3,137 Views)

As I told you before, the async timers are not affected by Sleep () function. Look at the simple program attached.

How do you see that the timer is not generating events? I see no functions to format the output message you are displaying, but this may be a typo in copy-pasting the code here.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 6
(3,047 Views)

Thank you for your test program! The reason for the missing ticks was a TCP function in the timer callback that I used. It seems like ProcessTCPEvents() is affected by Sleep() function. I used this Post to solve the problem. Since this post is old, is there now a better way to solve the problem than "busy waiting"? Anyway thank you for your help.

0 Kudos
Message 5 of 6
(3,037 Views)

There may be alternatives but generally speaking I consider that a brute Sleep (5000) is not a good way to implement a pause since it completely blocks the system, as you have already found. It's not just the problem of TCP events: what if the user needs to break the pause and / or the entire process on emergency? A would say that a rest with event processing is recommended in almost all situations.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(3,002 Views)