2013-02-25 62 views
5

में कार्य वर्ग का उपयोग करके मैं पावरहेल में कार्य वर्ग को अतुल्यकालिक रूप से चलाने के लिए उपयोग करने का प्रयास कर रहा हूं। लेकिन मैं निम्न अपवाद हो रही है:पावरहेल

Id      : 1 
Exception    : System.AggregateException: One or more errors occurred. ---> System.Management.Automation.PSInvalidOperationException: There is no Runspace available to 
         run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script 
         block you attempted to invoke was: Write-Host "hey!" 
          at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args) 
          at System.Threading.Tasks.Task.Execute() 
          --- End of inner exception stack trace --- 
         ---> (Inner Exception #0) System.Management.Automation.PSInvalidOperationException: There is no Runspace available to run scripts in this thread. You can 
         provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: 
         Write-Host "hey!" 
          at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args) 
          at System.Threading.Tasks.Task.Execute()<--- 

Status     : Faulted 
IsCanceled    : False 
IsCompleted   : True 
CreationOptions  : DenyChildAttach 
AsyncState    : 
IsFaulted    : True 
AsyncWaitHandle  : System.Threading.ManualResetEvent 
CompletedSynchronously : False 

मेरे कोड:

$delegate = [System.Action]{ Write-Host "Test" } 
[System.Threading.Tasks.Task]::Run($delegate) 

उत्तर

3

यह बहुत काम ले Task के साथ काम करने PowerShell प्राप्त करने के लिए होगा। Task पावरशेल के लिए सीधे इसके साथ काम करने के लिए निम्न स्तर के निर्माण का बहुत अधिक है।

पावरशेल संचालन करने के लिए अतुल्यकालिक रूप से jobs का उपयोग करें।

+3

रनस्पेस लागू करने के लिए अधिक कठिन हैं लेकिन [अधिक प्रदर्शनकर्ता] हो सकते हैं (http://learn-powershell.net/2012/05/13/using-background-runspaces-instead-of-psjobs-for-better-performance /)। – user2871239