01-05-2026 04:13 PM - edited 01-05-2026 04:19 PM
Our LabWindows/CVI 2017 app has UI buttons that run functions that send a bunch of messages to various devices. The message code is in a DLL. I used CmtGetLock() in that DLL so the background thread (which is polling the devices) cannot be interrupted by a user UI click.
Today I found a bug where blasting messages causes the app to crash (it just disappears, no error dialog). If I do the same from the LabWindows debugger, I get an error box with an unknown location (likely in the DLL, then, since no source code is available for that external code).
I only recently (last week or so) learned that the threadsafe variable macros do not lock when used from the same thread. I suspect the Cmt locking functions may be the same. The help sounds like it could be the case:
"When you finish using the thread lock, you must release the lock by calling CmtReleaseLock from the same thread that called this function. Only one thread can get ownership of the thread lock at a time. If a thread requests the lock while another thread owns the lock, the calling thread waits in this function until the owning thread releases the lock. You can call this function from the same thread more than once, but you must call CmtReleaseLock once for each time that you called this function."
If that is the case, I need to use a solution that will lock even when called from the same thread via a UI click.
Where should I be looking?
01-06-2026 02:57 PM
I have done some testing, and while button click callbacks do not normally interrupt themselves, the moment they call anything with a ProcessSystemEvents() that allows another instance of a click to start running the same callback. This is the main issue I am trying to resolve.
As a workaround, I am putting in extra code on each callback to block it from running if it is already running, but having a simple resource lock would be a nicer approach, I think.