# .NET and More > WPF, WCF, WF >  [RESOLVED] WPF Dispatcher BeginInvoke parameter mismatch

## szlamany

I am switching a program from using Winform threading to WPF Dispatcher threading - and I cannot get this C# syntax to work.

For the life of me I cannot see a problem with this code.



```
        private void startST(FSObject fsOb, string[] prefixes, string returnMessage)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new runSTDelegate(runST), new object[] { fsOb, prefixes, returnMessage });
            //System.Threading.ParameterizedThreadStart stParam = delegate { runST(fsOb, prefixes, returnMessage); };
            //System.Threading.Thread myThread = new System.Threading.Thread(stParam);
            //StatusInfo.Content = "..." + fsOb.DesiredAction + "[" + fsOb.SearchAction + "] " + StatusInfo.Content;
            //myThread.Start(fsOb);
        }
        private delegate void runSTDelegate(FSObject fsOb, string[] prefixes, string returnMessage);
        private void runST(FSObject fsOb, string[] prefixes, string returnMessage)
        {
            FSSerializer cl2 = new FSSerializer();
            fsOb.WhoFrom = "dfx";
            using (MemoryStream cqMS = new MemoryStream(100))
            {
.
.
.
            }
        }
```

startST breaks at the call to this.Dispatcher.BeginInvoke - which immediately returns this error:




> {"Parameter count mismatch."}
>    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
>    at System.Delegate.DynamicInvokeImpl(Object[] args)
>    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
>    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
>    at System.Windows.Threading.DispatcherOperation.InvokeImpl()
>    at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
>    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
>    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
> ...


For the life of me I cannot see where I am not telling it 3 parameters in each of the spots that need it.

----------


## jmcilhinney

You're calling BeginInvoke with three arguments, in which case the third argument you pass is passed as a single argument to the method to be invoked.  That's why there's a count mismatch: it's expecting three parameters and it's receiving just one, i.e. that whole object array.  If the method you're invoking has more than one parameter then you need to call the overload of BeginInvoke with four parameters.  The first argument to BeginInvoke is the first argument to the method to be invoked and the fourth argument to BeginInvoke is an object array containing the rest of the arguments to the method to be invoked.

----------


## szlamany

Thanks - that cleared up the problem...

Unfortunately - it's still blocking the UI thread - and I thought this was supposed to be async just like this.  Got to leave for a bit - guess I'll try and figure this out further later...

----------


## jmcilhinney

The point of BeginInvoke specifically IS to execute a method on the UI thread.  You call it from code being executed on a secondary thread in order to access the UI.  If your 'startST' method is being executed on the UI thread then BeginInvoke is achieving nothing.  Can we assume, then, that your aim is to execute 'runST' on a secondary thread?

----------


## szlamany

Yes - and how to do that with WPF seems to be a not-well-documented fact!

Any help would be greatly appreciated?

Or do I simply make a new DISPATCHER??

----------


## szlamany

I think I need to use a background worker - seems I can only find info about that in WPF research I'm doing...

http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

----------


## szlamany

I put it back to this and it's still blocking



```
        private void startST(FSObject fsOb, string[] prefixes, string returnMessage)
        {
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            //    new runSTDelegate(runST), fsOb, prefixes, returnMessage);
            System.Threading.ParameterizedThreadStart stParam = delegate { runST(fsOb, prefixes, returnMessage); };
            System.Threading.Thread myThread = new System.Threading.Thread(stParam);
            StatusInfo.Content = "..." + fsOb.DesiredAction + "[" + fsOb.SearchAction + "] " + StatusInfo.Content;
            myThread.Start(fsOb);
        }
```

----------


## szlamany

Argh - this was never really a problem - I chased a red-herring around because of the "work" I was doing on the "data returned" from my async operation.

But at any rate - the original commented out THREAD logic did work - it was not blocking.

What was blocking was the WPF work I was sending to the dispatcher to handle.

WPF handles this aspect differently.

In the end though - since I'm using 4.5 framework - I decided to build a TASKFACTORY and do a STARTNEW to run the async operation.

Actually feels a bit more "smooth" like this in the way the UI responds...

I am marking this RESOLVED and asking an ADMIN to move it to the WPF section - as it really was a WPF specific issue that start new down this path...

----------


## Hack

Moved TO WPF

----------

