LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Callbacks from two controls "at once"

Solved!
Go to solution

I need a bit of help with the basics of UI callbacks and threads.

 

Here is the scenario: There are two controls 

1. Numeric control with callback for EVENT_VAL_CHANGED

2. Button with callback for EVENT_COMMIT

 

RunUserInterface() is called once early in the program. User enters a value in the numeric control with the keyboard (just numbers), then they click on the button.

 

This fires the following events:

EVENT_VAL_CHANGED (numeric) --> numeric callback runs

EVENT_COMMIT (numeric)

EVENT_LEFT_CLICK (button)

EVENT_LEFT_CLICK_UP (button)

EVENT_COMMIT (button) --> button callback runs

EVENT_LOST_FOCUS (numeric (?))

EVENT_GOT_FOCUS (button)

 

Can parts of the button callback execute before the numeric callback completes?

 

Thanks!

0 Kudos
Message 1 of 8
(4,788 Views)
Callbacks are essentially normal C functions.
So you can call them like any normal C function.
Put a call to your ButtonCallback (...) function with appropriate parameters to simulate a button press within your NumericCallback (...) function.

You can discriminate a call originated from an UIR event from a direct call, by passing special values for eventData1, eventData2 or event parameters.
S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 8
(4,784 Views)

Thanks, ebalci!

 

I suppose I should have been more clear -- sorry. I do not want the button callback to execute before the numeric callback.

 

I have a weird bug report (of unknown reliability) that might be explainable of the button callback ran before the numeric callback. I can't replicate it in debug or release.

 

To be safe, I already did what you suggested, but in reverse. I put another direct call to the numeric callback at the start of the button callback. My hope was that if there were thread-related issues, that numeric callback would execute first inside the thread that eventually processes the button callback.

 

However I was unsure if my approach was addressing a physically possible scenario, in which the button callback executes some or all of its code before the numeric callback.  Do you know if that is possible (in the initial scenario outlined)?

 

Thanks,

Ian

0 Kudos
Message 3 of 8
(4,780 Views)
Well, if your numeric callback takes too much time and if system events are processed (either by an explicit call to ProcessSystemEvents() or implicitly by another function you call) within your numeric callback, and if the user presses the button before numeric callback execution finishes, then the button click might get processed and your button callback might get called before numeric callback is finished.

In the old days, since it is a good remedy for some issues and looked harmless, I used to "sprinkle" ProcessSystemEvents() all over the code.
But I started to use it much more carefully after experiencing its such "side efects" in multithreaded code.

I recently observed a similar behaviour in a TCP callback of mine.
If I called ProcessSystemEvents() within the callback, the code processes new data arrivals which occur while the callback is executing, before finishing its job with the data in its hand.

If you don't want the user to click the button before you are finished with the data typed in numeric control, you can disable (and then re-enable) the button in the numeric callback?
S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 8
(4,769 Views)

Ok, I think I need some official NI clarification: Do two separate UI callbacks triggered by one user action but different events run in the same thread, or not? (Pls see my OP for the an example)

 

elbaci has indicated that they could be. However, I can see one clue to the answer: the CVI help warns to be careful with using ProcessSystemEvents with a callback: "Take care when using ProcessSystemEvents, because it can allow other callback functions to execute before it completes." which seems to imply that by not calling ProcessSystemEvents (which is my situation) then the second callback will not complete before the first. 

 

(Just to be clear here: I do not use or care about ProcessSystemEvents for my question  -- it just was drawn into the discussion.)

 

Thanks,

Ian

0 Kudos
Message 5 of 8
(4,740 Views)

Hi Ian,
You may not be calling ProcessSystemEvents explicitly. However, some functions may force it. Even a Sleep() may cause other waiting events to get processed.
You might also find reading SetSleepPolicy function help useful.

These are not necessarily documented info. Most are from experience.

 

I will also enjoy an official clarification.
Ian it may help people if you can provide a code excerpt from your application..

S. Eren BALCI
IMESTEK
0 Kudos
Message 6 of 8
(4,736 Views)
Solution
Accepted by topic author Ian.W

To answer Ian's question, yes, all UI events are sent in the thread that owns the panel. Here's the relevant topic from the CVI help.

 

Luis

Message 7 of 8
(4,730 Views)

Thanks once again Luis!

 

It is a relief the know that I do not have to revisit all my code to search for places where the order of UI callback calls could be random ... so long as the events are raised in a consistent order. 🙂 

 

--Ian

0 Kudos
Message 8 of 8
(4,723 Views)