2013-02-12 49 views
2

背景

首先我創建了一個System.Action實例:異步調用一個System.Action例如在PowerShell中

PS C:\Users\KnutKristian> $a = [Action]{} 

在看看成員:

PS C:\Users\KnutKristian> $a | gm 

    TypeName: System.Action 

Name    MemberType Definition           
----    ---------- ----------           
BeginInvoke  Method  System.IAsyncResult BeginInvoke(System.AsyncCal... 
Clone    Method  System.Object Clone(), System.Object ICloneable... 
DynamicInvoke  Method  System.Object DynamicInvoke(Params System.Objec... 
EndInvoke   Method  void EndInvoke(System.IAsyncResult result)   
Equals   Method  bool Equals(System.Object obj)      
GetHashCode  Method  int GetHashCode()         
GetInvocationList Method  System.Delegate[] GetInvocationList()    
GetObjectData  Method  void GetObjectData(System.Runtime.Serialization... 
GetType   Method  type GetType()          
Invoke   Method  void Invoke()          
ToString   Method  string ToString()         
Method   Property System.Reflection.MethodInfo Method {get;}   
Target   Property System.Object Target {get;} 

BeginInvoke簽名:

PS C:\Users\KnutKristian> $a.BeginInvoke.OverloadDefinitions 
System.IAsyncResult BeginInvoke(System.AsyncCallback callback, System.Object ob 
ject) 

當我嘗試做開始調用我得到這個錯誤:

PS C:\Users\KnutKristian> $a.BeginInvoke($null, $null) 
Exception calling "BeginInvoke" with "2" argument(s): "The object must be a run 
time Reflection object." 
At line:1 char:1 
+ $a.BeginInvoke($null, $null) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ArgumentException 

指定的東西沒有幫助:

PS C:\Users\…> $a.BeginInvoke([AsyncCallback]{param([IAsyncResult] $a)}, '') 
Exception calling "BeginInvoke" with "2" argument(s): "The object must be a run 
time Reflection object." 
At line:1 char:1 
+ $a.BeginInvoke([AsyncCallback]{param([IAsyncResult] $a)}, '') 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ArgumentException 

一些異常信息:

PS C:\Users\KnutKristian> $Error[0].Exception.InnerException | gm 


    TypeName: System.ArgumentException 

Name    MemberType Definition           
----    ---------- ----------           
Equals   Method  bool Equals(System.Object obj), bool _Exception.... 
GetBaseException Method  System.Exception GetBaseException(), System.Exce... 
GetHashCode  Method  int GetHashCode(), int _Exception.GetHashCode()  
GetObjectData Method  void GetObjectData(System.Runtime.Serialization.... 
GetType   Method  type GetType(), type _Exception.GetType()   
ToString   Method  string ToString(), string _Exception.ToString()  
Data    Property System.Collections.IDictionary Data {get;}   
HelpLink   Property string HelpLink {get;set;}       
HResult   Property int HResult {get;}         
InnerException Property System.Exception InnerException {get;}    
Message   Property string Message {get;}        
ParamName  Property string ParamName {get;}        
Source   Property string Source {get;set;}       
StackTrace  Property string StackTrace {get;}       
TargetSite  Property System.Reflection.MethodBase TargetSite {get;} 


PS C:\Users\KnutKristian> $Error[0].Exception.InnerException | 
    fl Message, ParamName, InnerException -Force 


Message  : The object must be a runtime Reflection object. 
ParamName  : 
InnerException : 

問題

  1. 什麼信息

    The object must be a run time Reflection object.

    是否意味着在這方面?據我所知,這裏只有.NET對象。也許抱怨一些PowerShell的具體細節?

  2. 我怎樣才能成功運行BeginInvoke方法?異步

+1

嘗試創建一個PowerShell的對象,並給它一些屬性,並傳遞空的,與其。維基百科 「在計算機科學中,反射是一種計算機程序,以審查(見類型內省)和修改的結構和行爲在運行時的對象的(具體地,值,元數據,屬性和功能)的能力。[1 ] 反射在如Smalltalk高級虛擬機的編程語言和腳本語言最常用的,並且還明顯中鍵入或靜態類型的編程語言,比如Java,ML,Haskell和C#「。 – 2013-02-12 20:41:24

+0

' '''是['System.String'](http://msdn.microsoft.com/library/system.string.aspx)實例從['System.Object']導出(HTTP:// MSDN .microsoft.com /庫/ system.object.aspx)。我嘗試用'New-Object -TypeName PSObject -Property @ {Prop1 ='val'}'替換''''。沒有區別。問題1是上下文特定的。它爲什麼出現在這裏?對我沒有意義。 – knut 2013-02-12 21:37:52

回答

1

嘗試下面的代碼。

[Action]{}|%{$_.BeginInvoke($null, $null)} 


IsCompleted   : True 
AsyncDelegate   : System.Action 
AsyncState    : 
CompletedSynchronously : False 
EndInvokeCalled  : False 
AsyncWaitHandle  : System.Threading.ManualResetEvent 
NextSink    : 

請看看下面的文章,瞭解RunTimeType http://msdn.microsoft.com/en-us/library/ms172329.aspx

0

要運行的東西,你可以嘗試jobs(多進程方式,而不是如此高效的),或者你可以嘗試如圖this post創建RunspacePool

+0

我正在讀一本關於.NET和線程的書。我在PowerShell中做樣本。偉大的語言用來探索.NET庫。我想在PowerShell中運行'BeginInvoke'方法。我不想要替代品。 – knut 2013-02-13 08:42:54

+1

不確定PowerShell是測試多線程的好地方。你會使用C#和VS. – JPBlanc 2013-02-13 09:16:39