Results 1 to 9 of 9

Thread: [RESOLVED] WPF Dispatcher BeginInvoke parameter mismatch

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Resolved [RESOLVED] WPF Dispatcher BeginInvoke parameter mismatch

    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.

    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)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Windows.Threading.DispatcherOperation.Invoke()
    at System.Windows.Threading.Dispatcher.ProcessQueue()
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
    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.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
    at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
    at System.Windows.Threading.Dispatcher.Run()
    at System.Windows.Application.RunDispatcher(Object ignore)
    at System.Windows.Application.RunInternal(Window window)
    at System.Windows.Application.Run(Window window)
    at System.Windows.Application.Run()
    at WpfMentor.Navigation.App.Main() in d:\ACS Desktop\dfx\dfx\WpfMentor.Navigation\obj\Debug\App.g.cs:line 0
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(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)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    For the life of me I cannot see where I am not telling it 3 parameters in each of the spots that need it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,907

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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.

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,907

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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?

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: WPF Dispatcher BeginInvoke parameter mismatch

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

    Code:
            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);
            }

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: WPF Dispatcher BeginInvoke parameter mismatch

    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...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] WPF Dispatcher BeginInvoke parameter mismatch

    Moved TO WPF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width