03-19-2010 03:24 AM
Hi,
I want to use GetUserEvent() inside a loop for polling TIMER event, instead of using RunUserInterface() and TIMER calllback function.
but it seems GetUserEvent() can't get TIMER event. Are there any thing that I should notice before using GetUserEvent()?
I use a command button(PANEL_Add) to switch TIMER ON/OFF, the time interval is 1 second and TIMER is initially OFF.
PANEL_LED shows the status of TIMER ON/OFF flag.
PANEL_LED did turn on/off after I click the command button, but the counter value didn't change anyway while TIMER is enabled.
Thanks for answering~
Here is my code
==================================================================
int main (int argc, char *argv[])
{
int error = 0;
int done = FALSE;
int eventPanel;
int eventCtrl;
int panelHandle;
unsigned int Count=0;
int flag=0;
/* initialize and load resources */
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
if ((panelHandle = LoadPanel (0, "Timer.uir", PANEL)) <= 0)
return -1;
/* display the panel and run the user interface */
DisplayPanel (panelHandle);
while (done == FALSE){
GetUserEvent (1, &eventPanel, &eventCtrl);
switch (eventCtrl){
case PANEL_Add : flag = (flag==TRUE)? FALSE:TRUE;
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, flag);
SetCtrlVal(panelHandle, PANEL_LED, flag);
break;
case PANEL_TIMER : SetCtrlVal(panelHandle, PANEL_Counter, Count++);
if(Count > 19)
Count = 0;
break;
}
}
/* clean up */
DiscardPanel (panelHandle);
CloseCVIRTE ();
return 0;
}
and here is the .h file
==================================
#include <userint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define PANEL 1
#define PANEL_Counter 2
#define PANEL_Add 3
#define PANEL_LED 4
#define PANEL_TIMER 5
#ifdef __cplusplus
}
#endif
Solved! Go to Solution.
03-19-2010 03:36 AM - edited 03-19-2010 03:40 AM
Hi,
according to the help GetUserEvent obtains the next commit event or programmer-defined event from the GetUserEvent queue. A commit event occurs when the user changes the state of a hot or validate control or selects a menu item.
Place programmer-defined events in the GetUserEvent queue by calling QueueUserEvent.
Because the timer does not generate a commit event, you need to call QueueUserEvent; for this you will need to use the TimerCallback function that you didn't like... I see no other way than using the callback function of the timer
03-19-2010 04:50 AM
Thanks for answering~~
But this lead me to another question.
For testing QueueUserEvent(), I have modified my code and now it look likes the following
=========================================================
#define MyEvent1 1001
inside TIMER callback function
==========================
switch (event)
{
case EVENT_TIMER_TICK: QueueUserEvent(MyEvent1, panelHandle, PANEL_TIMER);
break;
}
return 0;
and now the polling loop becomes
==============================================
GetUserEvent (1, &eventPanel, &eventCtrl);
switch (eventCtrl){
case PANEL_Add : flag = (flag==TRUE)? FALSE:TRUE;
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, flag);
SetCtrlVal(panelHandle, PANEL_LED, flag);
break;
case MyEvent1 : SetCtrlVal(panelHandle, PANEL_Counter, Count++);
if(Count > 19)
Count = 0;
break;
}
what I thought is this should insert a new event into queue everytime after TIMER callback been executed,
and GetUserEvent() shold be able to get the event in polling loop.
But it didn't.
Did I miss anything again?
03-19-2010 04:59 AM - edited 03-19-2010 05:01 AM
I don't think that this is the most straightforward solution.... Why don't you put your statement into the timer callback, i.e.
==========================
switch (event)
{
case EVENT_TIMER_TICK:
SetCtrlVal(panelHandle, PANEL_Counter, Count++);
if(Count > 19) Count = 0;
break;
}
This makes life much more simple 🙂
03-19-2010 05:14 AM
yes, you are right
the code you post is the exact code I wrote at the first place~
To be curious, I just want to test how to use QueueUserEvent() but nothing else.
03-19-2010 05:34 AM
ok, in this case you need tp check the event status by using event_status = GetUserEvent (1,.. ,.. ) ...
from the help:
The event, if any, that occurred.
0 | No event. |
1 | Commit event occurred. |
1,000-10,000 | Event queued by QueueUserEvent. |
03-19-2010 08:05 AM
Be warned: if your panel has a menu bar your program will stay inside GetUserEvent() if the mouse cursor is sitting on a menu or menu item until the mouse is moved away or a menu item has been selected
(stupid tracking loop) .