LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

QueueUserEvent

I have a DLL that has exported functions. When one of those functions is called it creates threads (using CmtScheduleThreadPoolFunction after I create my own pool using CmtNewThreadPool ). Those threads (18) all go to the same function. ( MyThreadFunction ) In it, I listen for a 232 command, decode the info. and then call QueueUserEvent and pass the decoded data as the last parameter. The first is a define for user events == 1000 and the middle argument is the handle to the panel from the main thread. Doing it this way or even by using PostDeferredCall the USER EVENTS are never found. After the threads are created the main thread immediately enters a while loop with GetUserEvent. If I put the QueueUserEvent call (for debugging) in the function that creates th
e threads (before they're created) then the while loop will find that message but if it's coming from another thread it doesn't. I use only local variables in MyThreadFunction and none should be volatile. Any help would be appreciated. Thanks.
-G-
0 Kudos
Message 1 of 5
(3,296 Views)
The events generated in those threads will be specific to those threads, thus your main loop will not see them (this makes sense if you think about it, each thread could theoretically be running it's own UI, and if the events from those threads were seen by the main thread all kinds of not-so-fun things would end up happening). Luckily, CVI has a very nice mechanism for passing messages between threads, they are called Thread Safe Queues. You can enqueue items in one of these from your threads, and install a queue callback using CmtInstallTSQCallback with your main thread which will be called whenever items are placed in the queue.

Best of luck,
Ryan K.
0 Kudos
Message 2 of 5
(3,296 Views)
What Ryan says is correct: each thread has its own event queue.

I would only add that if you don't need all the functionality of a thread-safe queue (in other words, if you don't need to send data across threads) you can use the PostDeferredCallToThread function, which should have the behavior you're looking for.

Luis
NI
0 Kudos
Message 3 of 5
(3,296 Views)
I'm working on what Ryan said and currently when I call CmtWriteTSQData I get a return code of 2. The error codes are all negative so when I pass it to the CmtGetErrorMessage function it blows up. I find out why I get a 2 and what it represents. By the way, I tried the PostDeferredCall (since it's going to the main thread) with no avail. Thanks.
-G-
0 Kudos
Message 4 of 5
(3,296 Views)
Not entirely sure I understand your question. CmtWriteTSQData returns the number of data items written, this is described in the function panel, so 2 is a perfectly acceptable return value. There are some examples of using TSQs in your CVI directory, probably at something like
C:\Program Files\National Instruments\CVI70\samples\utility\Threading\ThreadSafeQueue\

Ryan
0 Kudos
Message 5 of 5
(3,296 Views)