Alex Rivera | Logout

What happened to Control.InvokeRequired in WPF?

Asked 2011-03-25T18:04:46.100
26

I am writing a small WPF app which spawns some number of threads to stress test a page of a website. I would like to output some information in the textbox of the wpf form of state of the thread I am manually starting from the main WPF thread. I was kind of expecting the WPF to have the same capabilities as far as synchronizing my threads back to the GUI thread through:

  if(this.InvokeRequired)
       this.Invoke(ProcessState);

However there is nothing of the sorts in the WPF... what are the options to do the output from a non-gui thread in WPF?

Edit: The issue now appears to be in the fact that I am not able to check for the method CheckAccess() of the Dispatcher object of my control. I am running the 3.5 version of .net and VS 2008 SP1.

Here is the full metadata of my Dispatcher Class (F12 on it):

  using MS.Internal.WindowsBase;
  using System;
  using System.ComponentModel;
  using System.Security;
  using System.Threading;

  namespace System.Windows.Threading
  {
     // Summary:
//     Provides services for managing the queue of work items for a thread.
public sealed class Dispatcher
{
    // Summary:
    //     Gets the System.Windows.Threading.Dispatcher for the thread currently executing
    //     and creates a new System.Windows.Threading.Dispatcher if one is not already
    //     associated with the thread.
    //
    // Returns:
    //     The dispatcher associated with the current thread.
    public static Dispatcher CurrentDispatcher { get; }
    //
    // Summary:
    //     Determines whether the System.Windows.Threading.Dispatcher has finished shutting
    //     down.
    //
    // Returns:
    //     true if the dispatcher has finished shutting down; otherwise, false.
    public bool HasShutdownFinished { get; }
    //
    // Summary:
    //     Determines whether the System.Windows.Threading.Dispatcher is shutting down.
    //
    // Returns
Edit
Report

1 Answer

3

If you're just converting WinForm Invoke to to WPF Dispatch, just a few small changes are needed to convert your WinForm code to the WPF code. This worked for my control named "_connection", and was rewritten to work in a WPF application.

// ---- Original WinForm code ----
delegate void SetIndicatorCallback(bool bVal);

private void SetIndicator(bool bVal)
{
   if (this._connection.InvokeRequired)
   {
      SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
      this.Invoke(d, new object[] { bVal });
   }
   else
   {
      if (bVal)
      {
           // Change the control's color, or whatever action is required.
      }

   }
}

// ---- WPF ----

// Still using a delegate 
delegate void SetIndicatorCallback(bool bVal);


private void SetIndicator(bool bVal)
    {
        if (this._connection.Dispatcher.CheckAccess())
        {
          if (bVal)
          {
                // Change the control' color, or whatever action is needed.
          }
        }
        else
        { 
           SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
           this.Dispatcher.BeginInvoke(d, new object[] { bVal });
        }
     }
answered 2012-11-18T18:19:00.487

Your Answer