Connect-Event, Disconnect-Event & Read-Event

Topics: Developer Forum, Project Management Forum
Developer
Apr 8, 2007 at 3:16 PM
Edited Apr 10, 2007 at 9:41 PM
I have some prototype code working for event binding and message loop style processing for ps scripts ;-)

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.
 
# $fsw = new-object system.io.filesystemwatcher
# $fsw.Path = "c:\temp"
# $fsw.EnableRaisingEvents = $true
# Add-PSSnapin pseventlib
# Connect-Event changed fsw -verbose
VERBOSE: Target is a FileSystemWatcher
VERBOSE: Event bound.
 
# Get-EventBinding
 
Variable                                EventName                               TypeName
--------                                ---------                               --------
fsw                                     Changed                                 FileSystemWatcher
 
# "test" > C:\temp\test.txt
# Read-Event
(immediately returns, doesn't wait - shows what 
events occured since connect-event connected them)
 
Event                                   Variable                                Args
-----                                   --------                                ----
Changed                                 fsw                                     System.IO.FileSystemEventArgs
Changed                                 fsw                                     System.IO.FileSystemEventArgs
 
# Read-Event
(nothing - event queue has been drained by last read)
 
# "test" > C:\temp\test.txt
# $events = Read-Event
# $events.Count
2
# $events
 
Event                                   Variable                                Args
-----                                   --------                                ----
Changed                                 fsw                                     System.IO.FileSystemEventArgs
Changed                                 fsw                                     System.IO.FileSystemEventArgs
 
 
# $events[0].args
 
                             ChangeType FullPath                                Name
                             ---------- --------                                ----
                                Changed c:\temp\test.txt                        test.txt

I'm working on a threaded version that can block until certain events occur, but as it stands, you can thread::sleep and poll for events yourself inside a while($true) {} loop.

I made an explicit choice to only support named objects (e.g. with an anchor in the variable: drive) so as to enable vbscript-like autobinding scenarios with functions named varname_eventname($source, eventargs$args).

Thoughts?
Developer
Apr 10, 2007 at 4:45 PM
I've created a new "binary-only" codeplex project for this one as it could get quite hairy: I don't want to impact the stability of PSCX. When it's mature enough, I will most likely roll it into Pscx.

http://www.codeplex.com/PSEventing
Coordinator
Apr 11, 2007 at 4:36 AM
That seems like a reasonable plan.
Developer
May 10, 2007 at 4:58 PM
Edited May 10, 2007 at 4:59 PM
pseventing 0.5 beta released

give her a whirl Keith and let me know what you think -- still some work to be done, but all the major stuff is there. I've linked to pscx from the project homepage (in related projects).
Coordinator
May 12, 2007 at 8:53 PM
I plan to check it out - just gotta carve out some time. :-)
Developer
May 21, 2007 at 3:55 PM

I'm going to have a look at this too ... I was looking at a couple of attempts to do this in script
by arildf and hanselman, but it's rather messy.
Developer
May 21, 2007 at 9:16 PM
Yeah, and script won't cover all avenues either. If the callback happens asynchronously, powershell will crap out with "no runspace available in this thread" error.

- Oisin