Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Event callback on thread

Reply
 
Old 12-06-2006   #1 (permalink)
William Stacey [C# MVP]


 
 

Event callback on thread

I am trying to use psh as an object test bench for a client and service
classes I have.

Couple issues so far:

1) Using async socket reads at client. psh, however, calls Methods on a new
thread and then exits. This causes socket read to error as the thread that
started the async operation is not allowed to exit so error is thrown by
socket. A hack workaround was to call the first beginreceive on a TP
thread, not the calling thread. In a "normal" client, this is almost never
an issue as you normally start with a main thread which never goes away
(i.e. ui thread, console main thread). Surely someone has seen this before
and has some advise. That does however beg the question of how this ever
works if you post another read in the read callback which is an TP/IOCP
thread that can get collected by the Pool.

2) My server object does not have issue #1 above, as it keeps its main
thread as the accept loop. However, it does async reads also and calls a
ReadReceive event for user code to handle, the callback is a TP thread as
normal (or I think an IOCP thread). Anyway, I get this exception in psh:


"System.Managment.Automation.PSInvalidOperationException:
There is no Runspace available to run the scripts in this thread.
You can provide one in the DefaultRunspace property..."

Is there anyway I can use a psh script as an event handler in this case?
TIA!

--
William Stacey [C# MVP]




My System SpecsSystem Spec
Old 12-08-2006   #2 (permalink)
William Stacey [C# MVP]


 
 

Re: Event callback on thread

Should I restate the question? tia

--
William Stacey [C# MVP]


My System SpecsSystem Spec
Old 12-12-2006   #3 (permalink)
Bruce Payette [MSFT]


 
 

Re: Event callback on thread

Sorry for the delay. Unfortunately the answer is that there's no easy way to
do what you want and no way to do it without writing some non-powershell
code (at least that I've thought of). PowerShell runspaces are
singlethreaded. If you try and execute a piece of code outside the runspace
(i.e. not on the runspace thread), you'll get the exception you saw:

"System.Managment.Automation.PSInvalidOperationException:
There is no Runspace available to run the scripts in this thread.
You can provide one in the DefaultRunspace property..."

So you'll have to write some code to forward the events to a runspace in
some manner. This type of event handling is one of the things we're actively
investigating for V.Next.

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message
news:upbQaloGHHA.3468@TK2MSFTNGP04.phx.gbl...
> Should I restate the question? tia
>
> --
> William Stacey [C# MVP]
>
>



My System SpecsSystem Spec
Old 12-12-2006   #4 (permalink)
William Stacey [C# MVP]


 
 

Re: Event callback on thread

Thanks Bruce.

--
William Stacey [C# MVP]


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How do the current thread get thread notification of OS intruption .NET General
CTP3: Thread Safety of Wait-Event cmdlet PowerShell
Start a new thread from an existing thread, which was started from atimer .NET General


Vista Forums 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 Ltd

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