2

Background

First I create a System.Action instance:

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

Having a look at the members:

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;}

The BeginInvoke signature:

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

When I try to do begin invoke I get this error:

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

Specifying stuff does not help:

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

Some exception information:

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 : 

Questions

  1. What does the message

    The object must be a run time Reflection object.

    mean in this context? There are only .NET objects here as far as I can see. Maybe complaining about some PowerShell specific details?

  2. How can I successfully run the BeginInvoke method?

knut
  • 4,699
  • 4
  • 33
  • 43
  • 1
    try creating a powershell object and giving it a few properties and pass that instead of null. From wikipedia "In computer science, reflection is the ability of a computer program to examine (see type introspection) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of an object at runtime.[1] Reflection is most commonly used in high-level virtual machine programming languages like Smalltalk and scripting languages and also in manifestly typed or statically typed programming languages such as Java, ML, Haskell and C#." – Christopher Douglas Feb 12 '13 at 20:41
  • `''` is a [`System.String`](http://msdn.microsoft.com/library/system.string.aspx) instance which derives from [`System.Object`](http://msdn.microsoft.com/library/system.object.aspx). I tried to replace `''` with `New-Object -TypeName PSObject -Property @{ Prop1 = 'val' }`. Did no difference. Question 1 is context specific. Why does it show up here? Makes no sense to me. – knut Feb 12 '13 at 21:37

2 Answers2

1

Try the below code.

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


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

Please look at the following article to read about RunTimeType http://msdn.microsoft.com/en-us/library/ms172329.aspx

  • Running this in Powershell 5 I get: Exception calling "BeginInvoke" with "2" argument(s): "The object must be a runtime Reflection object." At line:1 char:21 + $foo = [Action]{}|%{$_.BeginInvoke($null, $null)} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException – Tim Oct 31 '18 at 16:48
0

To run things asynchronously you can try jobs (multiprocess way, not so efficient) or you can try to create RunspacePool as shown in this post.

Community
  • 1
  • 1
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • I am reading a book about .NET and threading. I'm doing samples in PowerShell. Great language to explore the .NET library with. I want to run the `BeginInvoke` method in PowerShell. I don't want an alternative. – knut Feb 13 '13 at 08:42
  • 1
    Not sure that PowerShell is the good place for testing Multythreading. You'd beter use C# and VS. – JPBlanc Feb 13 '13 at 09:16