Powershell: BeginInvoke Parameter Count Mismatch

292 views Asked by At

I am trying to create a powershell script that pulls up a simple GUI form, displays "Loading..." in the title bar while it takes about 10-20 seconds to load the data, then finally change the title to "Please choose from the following options..." after loading.

I've been following this guide pretty much down to the letter. So far everything works, except for the fact that I get a "Parameter Count Mismatch" error when I execute this line:

$objListbox.BeginInvoke( [Action[string]] { param($Message) $objListbox.Items.Add = $Message }, $MyMessage )

Basically what I'm trying to do is load up about two thousand names into a multiselect listbox, then allow users to select the names they want to import elsewhere. So far everything works, I just have to hit continue on the Powershell ISE editor, but I'd like to get rid of this error all together.

1

There are 1 answers

0
Richard On

BeginInvoke is rather older than generics in the .NET framework, and considerably older than the delegate Action<T>. Specifically you cannot pass an instance of Action<T> to BeginInvoke.

Try casting to System.Delegate instead.