09-12-2022 09:37 AM
Hi,
My Vi sets and reads 24 mass flow controllers. The manufacturer provides VI files, in both read and set, VIs use call library function that is set to run in UI thread. The problem is that if I want to control 24 MFCs at the same time (that is 48 times calling) with a single VI, the front panel becomes nearly inoperable. Setting delay do not work. If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
However, if I make 48 copies of the dll file like MFC1Read.dll - MFC1Set.dll - MFC2Read.dll - MFC2Set.dll - ....... - MFC24Read.dll - MFC24Set.dll and 48 copies of VI that calls the dll from any thread, would it be same as running the dll file in UI thread?
Best regards
09-12-2022 09:58 AM
Your research may be flawed. It appears that the DLL function works just fine when running in any thread. This implies the DLL is "Threadsafe." There is no benefit to forcing the calls to hog the overworked single UI Thread unless the DLL is not threadsafe.
Modify the LabVIEW driver to allow the CLF nodes to run in any thread and open a conversation with the vendor about the library they provided..
09-13-2022 01:54 AM - edited 09-13-2022 01:57 AM
Well, it seems to work when you set the Call Library Node to reentrant. The problem with that is that only the programmer of the DLL knows if his/her DLL is really thread safe. If it isn't, it may usually work fine but occasionally produce strange results and/or crash.
Most programmers are sufficiently bothered about the Call Library Node to take their hands off from the VIs the moment it starts to work. That often leaves the Call Library Nodes to run in the UI thread (the only safe setting that will always work, safe from real bugs in the DLL or Call Library Node configuration), even though it might be perfectly fine to let them run reentrant.
But! Only someone with access to the source code of the DLL can tell if that is really going to be safe, unless of course the programmer of the DLL was a real Pro and actually mentioned that fact in the accompanying documentation. Without any such mentioning, you have to assume that the programmer actually has himself no clue about if something like thread safety would apply to his DLL and that experimenting with this setting is pretty much playing russian roulette. 😀
The idea about copying your DLL is most likely a dead end however. Most IO ports are protected resources that can't be opened multiple times without a lot of extra measures and from those multiple copies of the DLL, only one (the first one to get there) will be able to open the communication channel to the device.
09-13-2022 03:57 AM
@charmelony_ wrote:
If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
If it works smoothly (tested thoroughly) in run in any thread, why not go with it?
09-13-2022 06:35 AM
wiebe@CARYA wrote:
@charmelony_ wrote:
If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
If it works smoothly (tested thoroughly) in run in any thread, why not go with it?
After opening a dialog with the vendor. Those edge cases that Rolf alluded to are impossible to predict without a thorough investigation of the DLL source since some open pointers to refs may depend on OS garbage collection. "Just do it!" is fine for Nike sneakers but dangerous for Nike missiles.
09-13-2022 10:27 AM - edited 09-13-2022 10:28 AM
@JÞB wrote:
wiebe@CARYA wrote:
@charmelony_ wrote:
If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
If it works smoothly (tested thoroughly) in run in any thread, why not go with it?
After opening a dialog with the vendor. Those edge cases that Rolf alluded to are impossible to predict without a thorough investigation of the DLL source since some open pointers to refs may depend on OS garbage collection. "Just do it!" is fine for Nike sneakers but dangerous for Nike missiles.
Well, dlls are not sneakers nor missiles 😊.
I think it is in much harder to make a dll that isn't thread safe. That is, thread unsafe dlls are probably more complex.
Note that you can always set run in any thread if you only call the dll once at a time. So, if you put the dll in a non-reentrant VI, the dll will only be called once at the time, tread safety becomes irrelevant, and the UI thread won't be locked.
09-13-2022 12:20 PM
wiebe@CARYA wrote:
@JÞB wrote:
wiebe@CARYA wrote:
@charmelony_ wrote:
If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
If it works smoothly (tested thoroughly) in run in any thread, why not go with it?
After opening a dialog with the vendor. Those edge cases that Rolf alluded to are impossible to predict without a thorough investigation of the DLL source since some open pointers to refs may depend on OS garbage collection. "Just do it!" is fine for Nike sneakers but dangerous for Nike missiles.
Well, dlls are not sneakers nor missiles 😊.
On the other hand... I have written algorithms that must differentiate between the Nike footgear worn on the left foot from one worn on the right foot while ambulating on a treadmill. I have also written algorithms to test fusing systems for "Chemical Energetics," for an aerospace company.
There was a different level of confidence required.
Talk to the vendor.
09-13-2022 12:43 PM
@JÞB wrote:
wiebe@CARYA wrote:
@JÞB wrote:
wiebe@CARYA wrote:
@charmelony_ wrote:
If I change the thread of call library function to run in any thread, there is no lag in the front panel, everything runs smoothly. Unfortunately, I have no knowledge about dll and call library functions. On the other hand, after some research in the forum, I concluded that that dll should be run in UI thread.
If it works smoothly (tested thoroughly) in run in any thread, why not go with it?
After opening a dialog with the vendor. Those edge cases that Rolf alluded to are impossible to predict without a thorough investigation of the DLL source since some open pointers to refs may depend on OS garbage collection. "Just do it!" is fine for Nike sneakers but dangerous for Nike missiles.
Well, dlls are not sneakers nor missiles 😊.
On the other hand... I have written algorithms that must differentiate between the Nike footgear worn on the left foot from one worn on the right foot while ambulating on a treadmill. I have also written algorithms to test fusing systems for "Chemical Energetics," for an aerospace company.
There was a different level of confidence required.
Talk to the vendor.
Well, a possible outcome could be the dll isn't thread safe.
In that case (or if vendor doesn't reply), if you do want a responsive UI (who doesn't?), you can still put the dll on "run in any thread", and call the dll in a thread safe manner by forcing sequential calling (one way or another)...
09-14-2022 05:30 AM - edited 09-14-2022 05:36 AM
The vendor said they do not know. I let the Vi work enabling call library running in any thread. It has been two days, and so far there is no crash, all data are saved correctly. There is a case structure which makes dll calls are in sequence. That could be the workaround as you suggested.
I will report again couple days later. I conduct long term experiments and cannot risk any crash. That was the reason that I was hesitate running in any thread.
09-14-2022 06:11 AM
@charmelony_ wrote:
The vendor said they do not know. I let the Vi work enabling call library running in any thread. It has been two days, and so far there is no crash, all data are saved correctly. There is a case structure which makes dll calls are in sequence. That could be the workaround as you suggested.
I will report again couple days later. I conduct long term experiments and cannot risk any crash. That was the reason that I was hesitate running in any thread.
If you test the dll in a sequence structure (why not use the error wire??) you're not testing if the 'run in any thread' is harmful or not.
This means 'run in any thread' might not be safe. At some point, if you (or someone else) start calling the sequence n times (in two threads), crashes might appear.
If you want to be sure 'run in any thread' safe, you have to test in a way that is not safe...
Alternatively, put some comments (large orange or red text warnings) in your code that 'run in any thread' has not been tested for multiple threads...
Commenting these things in code and\or documentation is a good idea in general.