title: WPF ProgressBar - TargetParameterCountException
body: <p>I am making my first WPF application, where I use the Youtube .NET API to upload a video to Youtube using the ResumableUploader.</p>
  
  <p>This ResumableUploader works asynchronously and provides an event AsyncOperationProgress to periodically report its progress percentage.</p>
  
  <p>I want a ProgressBar that will display this progress percentage. Here is some of the code I have for that:</p>
  
  <pre><code>void BtnUpload_Click(object sender, RoutedEventArgs e) {
      // generate video
  
      uploader = new ResumableUploader();
      uploader.AsyncOperationCompleted += OnDone;
      uploader.AsyncOperationProgress += OnProgress;
      uploader.InsertAsync(authenticator, newVideo.YouTubeEntry, new UserState());
  }
  
  void OnProgress(object sender, AsyncOperationProgressEventArgs e) {
      Dispatcher.BeginInvoke((SendOrPostCallback)delegate {
          PgbUpload.Value = e.ProgressPercentage;
      }, DispatcherPriority.Background, null);
  }
  </code></pre>
  
  <p>Where PgbUpload is my progress bar and the other identifiers are not important for the purpose of this question.</p>
  
  <p>When I run this, OnProgress will be hit a few times, and then I will get a TargetParameterCountException. I have tried several different syntax for invoking the method asynchronously, none of which worked. I am sure the problem is the delegate because if I comment it out, the code works fine (but the ProgressBar isn't updated of course).</p>
  
  <p>Here is the Exception Detail (partially in French):</p>
  
  <blockquote>
    <p>System.Reflection.TargetParameterCountException
    was unhandled   Message=Nombre de
    paramètres incorrects.<br>
    Source=mscorlib   StackTrace:
           à System.Reflection.RuntimeMethodInfo.Invoke(Object
    obj, BindingFlags invokeAttr, Binder
    binder, Object[] parameters,
    CultureInfo culture, Boolean
    skipVisibilityChecks)
           à System.Delegate.DynamicInvokeImpl(Object[]
    args)
           à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
    callback, Object args, Boolean
    isSingleParameter)
           à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object
    source, Delegate callback, Object
    args, Boolean isSingleParameter,
    Delegate catchHandler)
           à System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate
    callback, Object args, Boolean
    isSingleParameter, Delegate
    catchHandler)
           à System.Windows.Threading.DispatcherOperation.InvokeImpl()
           à System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
    state)
           à System.Threading.ExecutionContext.runTryCode(Object
    userData)
           à System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
    code, CleanupCode backoutCode, Object
    userData)
           à System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback
    callback, Object state)
           à System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback
    callback, Object state)
           à System.Windows.Threading.DispatcherOperation.Invoke()
           à System.Windows.Threading.Dispatcher.ProcessQueue()
           à System.Windows.Threading.Dispatcher.WndProcHook(IntPtr
    hwnd, Int32 msg, IntPtr wParam, IntPtr
    lParam, Boolean&amp; handled)
           à MS.Win32.HwndWrapper.WndProc(IntPtr
    hwnd, Int32 msg, IntPtr wParam, IntPtr
    lParam, Boolean&amp; handled)
           à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object
    o)
           à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
    callback, Object args, Boolean
    isSingleParameter)
           à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object
    source, Delegate callback, Object
    args, Boolean isSingleParameter,
    Delegate catchHandler)
           à System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate
    callback, Object args, Boolean
    isSingleParameter, Delegate
    catchHandler)
           à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority
    priority, TimeSpan timeout, Delegate
    method, Object args, Boolean
    isSingleParameter)
           à System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority
    priority, Delegate method, Object arg)
           à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr
    lParam)
           à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp;
    msg)
           à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
    frame)
           à System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame
    frame)
           à System.Windows.Threading.Dispatcher.Run()
           à System.Windows.Application.RunDispatcher(Object
    ignore)
           à System.Windows.Application.RunInternal(Window
    window)
           à System.Windows.Application.Run(Window
    window)
           à System.Windows.Application.Run()
           à WpfApplication3.App.Main() dans h:\razor\documents\visual studio
    2010\Projects\WpfApplication3\WpfApplication3\obj\x86\Debug\App.g.cs:ligne
    0
           à System.AppDomain._nExecuteAssembly(Assembly
    assembly, String[] args)
           à System.AppDomain.ExecuteAssembly(String
    assemblyFile, Evidence
    assemblySecurity, String[] args)
           à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           à System.Threading.ThreadHelper.ThreadStart_Context(Object
    state)
           à System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback
    callback, Object state)
           à System.Threading.ThreadHelper.ThreadStart()
    InnerException:</p>
  </blockquote>
  
  <p>Thanks for any help.</p>
  
  <hr>
  
  <h2><strong>Edit: I just found out that if I don't use the Dispatcher and just call set the value directly, it works fine! Is OnProgress called on main UI thread? How can that be?</strong></h2>