![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 userdecides 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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Execute clean-up code in response to Ctrl-C? On Dec 18, 6:35 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Execute clean-up code in response to Ctrl-C?
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 Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Execute clean-up code in response to Ctrl-C? On Dec 18, 12:27 pm, Duncan Smith <DSmith1...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: Execute clean-up code in response to Ctrl-C? On Dec 18, 11:35 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guest | Re: Execute clean-up code in response to Ctrl-C? On Dec 19, 11:01 am, Duncan Smith <DSmith1...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
| 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 |