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 - Re: Invoke Scriptblock with parameters in a new thread

Reply
 
Old 01-13-2009   #1 (permalink)
Markus


 
 

Re: Invoke Scriptblock with parameters in a new thread


Quote:

> I'm not a dev, but just wanted to point out that PowerShell v1 only
> supports running under MTA.
>
> PowerShell v2 (currently at CTP3) supports STA (but still defaults as MTA).
>
> Marco
>

Hi Marco,
I'm really pleased that you reflect on my problem!!

I still tried to run the threads under MTA. But the result was, that the
threads did not run parallel. I also tried the PowerShell V2 CTP.

I found an example to create such a MTA but the used waithandle (from type
ManualResetEvent) does not allow the threads to run parallel.
I'm not very familiar with MTA. Perhaps you can help me ?

Here is the try:

protected override void BeginProcessing()
{
this.m_Runspace = Runspace.DefaultRunspace;
}


protected override void ProcessRecord()
{
//Create a Waithandle
m_Waithandle = new ManualResetEvent(false);

//Start a new thread in a multithreaded apartment
Thread t = new Thread(new ThreadStart(DoWork));
t.SetApartmentState(ApartmentState.MTA);
t.Start();

//Block the current thread
m_Waithandle.WaitOne();
}


public void DoWork()
{
//Set the runspace
Runspace.DefaultRunspace = this.m_Runspace;

//Invoke the Scriptblock
this.m_myScriptBlock.Invoke(this.m_Array);

if (m_Waithandle != null)
{
//Allow the thread to work
m_Waithandle.Set();
}
}


Thank you


My System SpecsSystem Spec
Old 01-13-2009   #2 (permalink)
Markus


 
 

Re: Invoke Scriptblock with parameters in a new thread

I just want to add that the only purpose is to start the command:

this.m_myScriptBlock.Invoke(this.m_Array);

in two or more parallel asynchronous threads.
That means: Every time the Cmdlet is called, the command will be executed in
a new thread.

Thank you for your help.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Start a new thread from an existing thread, which was started from atimer .NET General
Powershell Invoke-Item and cmd line parameters PowerShell
Invoke-Item and passing command line parameters PowerShell
Functions and ScriptBlock parameters? PowerShell
Scriptblock Parameters? PowerShell


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