LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect focus change between apps

Solved!
Go to solution

LV2013, Win7

 

I have a lengthy operation that I need to so.  Let's call it a "scan".

I start at a particular folder on disk, and look for folders within that, and folders within those, etc.  I'm looking for particular files (*.lvlibp).  When I find one, I need to open it, look for particular VIs within it, and check them for executability (broken), and correct ConnPane, etc.  I put all the qualifying items into a menu (in the menu bar) as submenus, and submenus of those, etc.

 

I'm doing the scan in a separate thread, so it's not like I'm blocking anything, but it does take away CPU time that I would rather not use.

 

If I do the scan only once, then the user can go to Explorer, move a file in place, or rename a folder, and come back and I miss the changes.

 

Right now, I'm doing it whenever this one window comes up (the one that the menu applies to).  But that means switching windows around  within my app causes it to scan again, when there's no chance of a new file appearing.

 

I would like to do this scan only when necessary, which I define as when I come back from some other program (WIndows explorer for example) to this one window in my LabVIEW app.    I'm willing to accept the fact that Windows Scripts, or remote operations might sneak a file in there.  I'm not looking to be bulletproof, just accommodating the most common scenarios.

 

I don't want to do some sort of polling - the whole app is event-driven, and I want to keep it that way.

 

So, how do I do this?  

 

If I look at the VI ACTIVATION event, it looks like it happens for ANY VI getting activated.  I can attach a name-comparison and detect when the right window is coming forward, but still, just because this window comes forward doesn't mean I need to scan.

 

If I look at the APP ACTIVATION event, it seems to NEVER be called. Even in an EXE, the event never seems to happen.

 

If I had an APP DEACTIVATION event, I could set a flag when that happened, and the next time the right window comes up, I could check the flag and scan if necessary. 

 

But I have no such event.

 

Any ideas on how to do this?

 

 

 

 

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 1 of 6
(4,242 Views)

If you're on Windows, you can use .NET to register for changes on the folder. I have no experience with this, so I can't give you any specifics (other than to say that I do remember some examples here. I might have even posted some of them). A quick search shows FileSystemWatcher as one class that handles it. I don't know if it's the only one or the best one.

 

Assuming you manage to register for the events, that executes a callback VI which you can use to trigger the reloading.


___________________
Try to take over the world!
Message 2 of 6
(4,230 Views)

There is a nugget by SteveChandler on this topic (FileSystemWatcher).

 

Ben64

Message 3 of 6
(4,222 Views)

I saw the SteveChanler nugget, but it complains about .NET assembly version (it wants 2.0, and I have 4.0 on my system).  

When I run it, it generates error 1172, then does nothing until I STOP the program, where it reports the error.

 

The error message is particularly useless, referring to a non-existent information, and telling me to go search.

 

Exception.PNG

 

 

I found another example elsewhere, called File Watch.  It's less comprehensive (has only the CREATED event), but actually works.

 

But it occurs that I don't want to watch all the time.  I certainly don't want to re-scan on every change (once when you create a new folder, once when you put the file in, one when you rename it)

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 4 of 6
(4,208 Views)
Solution
Accepted by CoastalMaineBird

I think what I will do is simply monitor the LAST MOD TIME of the parent folder.

 

When I do a scan, I will stash the LMT of the parent.

Then, every time the WINDOW is activated, I will check the LMT of the folder against my stash.

 

If it's different, then I scan again.

 

Checking the LMT takes about 50 uSec on my system, a small enough price to pay.  And I will only do it when the window comes back up, so I won't notice every little detail of what you do in there, and I don't have to dig into .NET and assemblies and versions and useless error messages and such.

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 5 of 6
(4,201 Views)

Sounds like the FileWatcher is the best option, but to address the original question I do have something to help.

 

Attached is two VIs saved in 2013.  One will return the window title of the window with focus.  And the other will wait for a window to be active, that has the title you want.  You could probably use some timeout and generate a user event when the window goes from not your window, to your window being active.

0 Kudos
Message 6 of 6
(4,182 Views)