Mode_Locked Posted September 6, 2020 Report Posted September 6, 2020 My code watches for an event where my Toshiba USB stick is unplugged. If the event does not occur within 30 secs, the code times out. However, in the case that there is no disconnection of the USB stick (no event generated), I can't end the program before the timeout period of 30 sec. Is there a way to end the program before the timeout period? Any help is appreciated. Thanks in advance! WMI_USBStorage_Event_withDeviceIDandTimeOut.vi Quote
LogMAN Posted September 6, 2020 Report Posted September 6, 2020 (edited) Welcome to Lava! There is no way to intercept WaitForNextEvent, other than generating the event it is waiting for or the timeout occurring. It is, however, possible to handle the event directly in LabVIEW. Are you familiar with .NET Event Callbacks? Use the Register Event Callback function to execute a callback VI to generate a user event for an Event Structure. The event structure can then take care of the timeout as well as other events that may end execution prematurely. Here is an example. For simplicity reasons I didn't include error handling and the callback VI, but you should be able to work the rest out from here. Disclaimer: I haven't tested this, but I'm certain it will do what you want. Edited September 6, 2020 by LogMAN 2 Quote
Mode_Locked Posted September 6, 2020 Author Report Posted September 6, 2020 (edited) Thanks for taking a stab at this, LogMAN! Unfortunately, that did not work. Looks like there is something special about 'WaitForNextEvent' that recognizes system level WMI events that LabVIEW is not able to see. I'm no WMI expert, but I looked at this in terms of dataflow by highlighting execution on the block diagram. It appears that the data just flows through 'Reg Event Callback' node without waiting for the event as soon as the program is run. I'm going to assume that there is a rather low probability of you having a Toshiba USB stick I modified the code to where it detects the launch of Notepad. This way, you could test it on your system if interested. Again, thank you for your help. WMI_NotepadLaunchEvent_withTimeOut.vi Edited September 7, 2020 by Mode_Locked Quote
Neil Pate Posted September 7, 2020 Report Posted September 7, 2020 8 hours ago, Mode_Locked said: Thanks for taking a stab at this, LogMAN! Unfortunately, that did not work. Looks like there is something special about 'WaitForNextEvent' that recognizes system level WMI events that LabVIEW is not able to see. I'm no WMI expert, but I looked at this in terms of dataflow by highlighting execution on the block diagram. It appears that the data just flows through 'Reg Event Callback' node without waiting for the event as soon as the program is run. I'm going to assume that there is a rather low probability of you having a Toshiba USB stick I modified the code to where it detects the launch of Notepad. This way, you could test it on your system if interested. Again, thank you for your help. WMI_NotepadLaunchEvent_withTimeOut.vi 13.21 kB · 1 download Reg Event Callback is not the thing that waits, it is (as the name implies) merely the registration. It should return immediately. Did you create the callback VI? Quote
Mode_Locked Posted September 7, 2020 Author Report Posted September 7, 2020 Yes, callback vi created. Please see attached code. Modified for notepad.exe process launch. Thanks! WMI_NotepadLaunch_LVRegEvent.vi NotepadCallBack.vi Quote
dadreamer Posted September 7, 2020 Report Posted September 7, 2020 Should ManagementEventWatcher.Start method be called in order for it to run asynchronously? Something like this: WMI_USBStorage_Event_withDeviceIDandTimeOut.viCB.vi Tested this sample on my own USB drive and it works fine. 1 Quote
Mode_Locked Posted September 7, 2020 Author Report Posted September 7, 2020 (edited) dadreamer, you nailed it! Works like a charm now. Thank you. Edited September 7, 2020 by Mode_Locked Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.