Example Code

Monitor Directory for Changes with Events instead of Polling in LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

In many applications file processing applications, we do one of two things: 1.) Programatically poll a diretory for changes in LV, or 2.) We wait to start our processing application and manually start our labview program once the file has been added.  We can have LabVIEW monitor a directory, but by using events instead of polling to save on CPU resources.

 

Description

This example shows how we can use directory change notifications within the Windows API to monitor a folder for a change in a certain directory.  We utilize the FindFirstChangeNotification to set up our events to monitor.  We then utilize the wait functions to trigger a LV event when a change has been made to the directory.  For more information on the Windows API, visit the MSDN.

 

Requirements

  • LabVIEW 2013 (or compatible).

 

Steps to Implement or Execute Code

  1. To run this code, simply unzip the attached project
  2. Run MainVI.vi
  3. To observe the events, copy and paste any file into the 'Example Folder'
  4. You'll see an event trigger and a popup windows for each file added

 

Additional Information or References

To implement this code are 3 VIs.  The Create monitor setups up the change notification handle for a directory passed to it.  The Monitor Alert is what is reponsible for actually monitoring the file.  This VI accepts a reference to a string control to event trigger when a folder has been changed.  Lastly, there is the destroy monitor, this VI destorys the directory handle and stops the execution of the Monitor alert.vi.

 

Main VI snippet - demonstrates the registration of events to LV event structure

MainSnippet.png

Monitor VI snippet

MonitorSnippet.png

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Cole R.
National Instruments
Software Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
testingHotAir
Active Participant
Active Participant
on

Hi Cole,

 

Thanks for posting this example, it was very helpful.  I do have a couple comments based on issues I had integrating this into an application.

 

Basically what I did was wrap this functionality inside an Actor so I can drop this into any top level actor to monitor a folder.  Common task, good thing to make reusable.  It worked fine in the development environment, but then would hang when built.  I noticed you had the FindFirstChangeNotificationA call in CreateMonitor set to run in the UI thread.  Changing this to Any Thread solved my hanging issue, though I don't really know why.

 

Second, you shut down the MonitorAlert by looking for a response of 4294967295, but the response from this function is undefined when a handle is closed.  (See https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx)  I think the 4294967295 response is dependent on some timeout or something, because changing the execution timing of the DestroyMonitor code changes the response to 0, but only sometimes.  To get around this, I look at the response from the FindNextChangeNotification function call, since it will always be 0 if the handle has been closed.  This seems to be reliable.

 

Thanks again,

Scott

CLAD
JRiggles
Member
Member
on

Hi Cole,

 

I know this is an older example, but I stumbled across it after a search and I'm working with it in LV18. I was just curious what the effect / advantage of registering the string value change as a dynamic event is, as opposed to just using a standard "Value Change" event.

 

Thanks!

 

- JR