Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Execute clean-up code in response to Ctrl-C?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-18-2007   #1 (permalink)
Duncan Smith
Guest


 

Execute clean-up code in response to Ctrl-C?

I have a script that creates a TCP listening socket, which sits in a
for(; loop and sends data to clients. All works well until the user
decides to stop the process by hitting Ctrl-C, unfortunately the
listening socket remains active until the powershell window is exited
- meaning the script cannot be called again because the listening
socket already exists.

Is there a way to execute a quick clean-up script-block on Ctrl-C, or
some other way to ensure the listening socket is taken down once the
script finishes (rather than the shell itself exiting).

I think it's too big an 'ask' to request the user to re-start the
shell before entering a command...

Many thanks,

Duncan

My System SpecsSystem Spec
Old 12-18-2007   #2 (permalink)
Oisin Grehan
Guest


 

Re: Execute clean-up code in response to Ctrl-C?

On Dec 18, 6:35 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
Quote:

> I have a script that creates a TCP listening socket, which sits in a
> for(; loop and sends data to clients. All works well until the user
> decides to stop the process by hitting Ctrl-C, unfortunately the
> listening socket remains active until the powershell window is exited
> - meaning the script cannot be called again because the listening
> socket already exists.
>
> Is there a way to execute a quick clean-up script-block on Ctrl-C, or
> some other way to ensure the listening socket is taken down once the
> script finishes (rather than the shell itself exiting).
>
> I think it's too big an 'ask' to request the user to re-start the
> shell before entering a command...
>
> Many thanks,
>
> Duncan
Hi Duncan,

My PSEventing project allows you to capture CtrlC as an event to be
processed, preventing powershell from stopping your script. It's also
useful for working with other events on the socket objects. The
project itself is at http://www.codeplex.com/pseventing and my blog
entry is at:

http://www.nivot.org/2007/12/05/Fore...owerShell.aspx

"Here's another interesting use for my PowerShell Eventing Snap-In,
where I'm simulating unix-style foreground/background tasks. In this
case, the task is a large download using System.Net.WebClient. Just
dot-source the script below and start a download using the Start-
Download function, passing the url to the large file and the local
path where to save your file (be sure to fully qualify the save path).
The download will start immediately and show a progress bar with bytes
remaining and a percentage, however you can hit Ctrl+C at any time and
the download will continue in the background. You can get back to
PowerShell tasks, and bring back up the progress bar by invoking Show-
Progress at any time. Use Stop-Download to cancel the currently active
download. Only one download can be active at a time, but this could
easily be extended to support a pool of downloads (using multiple
WebClient objects)."

Hope this helps,

- Oisin / x0n
My System SpecsSystem Spec
Old 12-18-2007   #3 (permalink)
Duncan Smith
Guest


 

Re: Execute clean-up code in response to Ctrl-C?

Quote:

>
> My PSEventing project allows you to capture CtrlC as an event to be
> processed, preventing powershell from stopping your script. It's also
> useful for working with other events on the socket objects. The
> project itself is athttp://www.codeplex.com/pseventingand my blog
> entry is at:
>
> http://www.nivot.org/2007/12/05/Fore...ppableDownload...
>
Thanks Oisin, my script was already using PSEventing to get the data
in - very useful. I hadn't seen Start-KeyHandler before, and that's
pretty much what I was after...

I haven't had much of a look at PS2 yet, but was hoping that
PSEventing might have made it into the release (a bit like the boost
libraries in TR1)

There are a few things now that I would have had to abandon in PS1 if
it weren't for PSEventing.

Maybe PS2 can handle events from COM objects so I don't have to write
thinn proxy stubs to pass the events on in .NET (that would be the
icing on the cake!)

Much appreciated,

Duncan
My System SpecsSystem Spec
Old 12-18-2007   #4 (permalink)
Oisin Grehan
Guest


 

Re: Execute clean-up code in response to Ctrl-C?

On Dec 18, 12:27 pm, Duncan Smith <DSmith1...@xxxxxx> wrote:
Quote:
Quote:

> > My PSEventing project allows you to capture CtrlC as an event to be
> > processed, preventing powershell from stopping your script. It's also
> > useful for working with other events on the socket objects. The
> > project itself is athttp://www.codeplex.com/pseventingandmy blog
> > entry is at:
> >
> Thanks Oisin, my script was already using PSEventing to get the data
> in - very useful. I hadn't seen Start-KeyHandler before, and that's
> pretty much what I was after...
>
> I haven't had much of a look at PS2 yet, but was hoping that
> PSEventing might have made it into the release (a bit like the boost
> libraries in TR1)
>
> There are a few things now that I would have had to abandon in PS1 if
> it weren't for PSEventing.
>
> Maybe PS2 can handle events from COM objects so I don't have to write
> thinn proxy stubs to pass the events on in .NET (that would be the
> icing on the cake!)
>
> Much appreciated,
>
> Duncan
Glad to hear it's helping you. PSEventing has full help for each
cmdlet, so use -? per cmdlet and/or get-help about_pseventing for more
general help.

As far as I've heard, eventing in some shape or form is coming to v2,
but it's not in the current CTP.

- Oisin
My System SpecsSystem Spec
Old 12-19-2007   #5 (permalink)
Duncan Smith
Guest


 

Re: Execute clean-up code in response to Ctrl-C?

On Dec 18, 11:35 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
Quote:

> I have a script that creates a TCP listening socket, which sits in a
> for(; loop and sends data to clients. All works well until the user
> decides to stop the process by hitting Ctrl-C, unfortunately the
> listening socket remains active until the powershell window is exited
> - meaning the script cannot be called again because the listening
> socket already exists.
>
> Is there a way to execute a quick clean-up script-block on Ctrl-C, or
> some other way to ensure the listening socket is taken down once the
> script finishes (rather than the shell itself exiting).
>
> I think it's too big an 'ask' to request the user to re-start the
> shell before entering a command...
>
> Many thanks,
>
> Duncan
Thanks Oisin,

Just been looking at the www.nivot.org website, the code samples look
really good - do you have an IDE that does the syntax highlighting,
line numbering and alternate line shading for PS, or are they
generated for the web-site from XSL or something?

Thanks,

Duncan
My System SpecsSystem Spec
Old 12-19-2007   #6 (permalink)
Oisin Grehan
Guest


 

Re: Execute clean-up code in response to Ctrl-C?

On Dec 19, 11:01 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
Quote:

> On Dec 18, 11:35 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
>
>
>
>
>
Quote:

> > I have a script that creates a TCP listening socket, which sits in a
> > for(; loop and sends data to clients. All works well until the user
> > decides to stop the process by hitting Ctrl-C, unfortunately the
> > listening socket remains active until the powershell window is exited
> > - meaning the script cannot be called again because the listening
> > socket already exists.
>
Quote:

> > Is there a way to execute a quick clean-up script-block on Ctrl-C, or
> > some other way to ensure the listening socket is taken down once the
> > script finishes (rather than the shell itself exiting).
>
Quote:

> > I think it's too big an 'ask' to request the user to re-start the
> > shell before entering a command...
>
Quote:

> > Many thanks,
>
Quote:

> > Duncan
>
> Thanks Oisin,
>
> Just been looking at thewww.nivot.orgwebsite, the code samples look
> really good - do you have an IDE that does the syntax highlighting,
> line numbering and alternate line shading for PS, or are they
> generated for the web-site from XSL or something?
>
> Thanks,
>
> Duncan- Hide quoted text -
>
> - Show quoted text -
Hey Duncan,

Thanks for the positive comments. I use:

http://www.thecomplex.plus.com/highlighter.html

....for syntax highlighting - pretty nice, isn't it? I then use windows
live writer to compose posts (for nivot.org which uses dasblog 1.9)
and insert the sample in HTML view. I then copy the html source of the
whole post to my blog on powershellcommunity.org which uses dot net
nuke for blogging (which doesn't provide any APIs for posting yet, so
live writer doesn't work).

- Oisin / x0n

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remote desktop does not sent ctrl+alt+left or ctrl+alt+right kevand Network & Internet 0 08-22-2008 10:56 AM
Execute error: Type Mismatch: 'Execute' kellym-global.co.uk VB Script 2 08-21-2008 10:09 AM
Copy (Ctrl+C) and Paste (Ctrl+V) not working David McRitchie Live Mail 7 06-13-2008 07:42 AM
Initial CTRL+C response is slow J. McConnell PowerShell 7 10-25-2006 07:54 PM
Ctrl-Alt-Delete / Ctrl-Shift-Esc--------Task Manager James Welch Vista General 0 09-19-2006 08:38 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51