Monitor a folder and send alerts, problem with gui part

Hi guys!

I have folder that will get textfiles wich I want to present to the user. But only when the user have the application started.

I done this so far:

3 functions---- (function Main is the function that runs when you start the appliction)

function Main:

  1. Start a gui part that is just a windows that says “ALERTS” (created with primal forms)
  2. Starts the function “PollAlerts”

function PollAlerts:

  1. Searches a directory / folder after new files
  2. When it find a new file it start function “SendMessage” with the containt of the file as message.
  3. writes a .hash files that contains the md5sum for the file so it wont send it to “SendMessage” function again

function SendMessage:

  1. get the message to send from $args and then creates a pop-up message with the help of gui that I created with primal forms
  2. When the users clicks the button “markera meddelandet som läst” the scripts writes to the log and closes the pop-up

And som more logging… and stuff.

My problem is this:
The first gui parts hangs when the monitoring beginns. How should I do to seperate that part and still close it when the gui closes?
-Tried start-job but then the script can’t create the pop-up message.
-Tried to seperate the “monitoring” to another script and start it from the main script with “start-process”. Then it does not work either…

What to do??

Best Regards

It’s hard to tell without seeing your code, but you’re probably going to have better luck using event-driven monitoring rather than writing your PollAlerts function. If this were a .NET application, I’d say that your PollAlerts function was blocking your GUI thread, but I’m not sure how well that concept applies to a PowerShell script.

Anyhow, do a web search for “PowerShell FileSystemWatcher” and you’ll find lots of examples for using .NET’s System.IO.FileSystemWatcher class in conjunction with PowerShell’s Register-ObjectEvent cmdlet.

I’ll second that. Unless you’ve got a TON of files being created in a short period of time (e.g., seconds), the FileSystemWatcher .NET component is much more reliable. It fires an event when new files appear, and you can register to receive that event and execute code in response to it. It’s much more reliable, and lower-overhead, than a polling routine.

Thanks for your answers guys!

I managed to solve it before I had a chance to read your answers. I Will look into the “FileSystemWatcher” when we have our next release.

Best regards