Confused accessing delegate from WPF ui thread

My apologies but I can't see what I am doing wrong / not understanding? I have declared a delegate such as:

namespace ABC
{
   public delegate void GenerateView();

   public class X
   {
      public X()
      {
         GenerateView handler = GenerateViewMethod;
      }

      public void GenerateViewMethod()
      { .... }

    }
 }

Then in the ui I call it as such:

private readonly X MyXClass;
Dispatcher.BeginInvoke(MyXClass.???) //handler isn't an option?

The only method that shows is GenerateViewMethod and of course that isn't my delegate.

So obviously I am not exposing the delegate properly but I'm not clicking on what is missing.

Thank you for the assistance


Update further information

I have a WPF Ui and when the user clicks the only button I simply want to do some processing in a different thread and not lock the ui. Pretty simple little test for me as I'm learning.

Jon Skeet
people
quotationmark

The reason your current code doesn't work is that handler is just a local variable within the constructor.

While you could add a public property, if the point is just for external code to be able to get a delegate which calls GenerateViewMethod, you don't need anything extra for that - the other code can just use a method group conversion. For example:

Action action = MyXClass.GenerateViewMethod;
Dispatcher.BeginInvoke(action);

Or:

Dispatcher.BeginInvoke((Action) MyXClass.GenerateViewMethod);

Or if you're using DispatcherExtensions, you can simply use:

Dispatcher.BeginInvoke(MyXClass.GenerateViewMethod);

The reason the DispatcherExtensions class helps here is that the methods declared by Dispatcher itself only take Delegate parameters, so the compiler needs to be told what kind of delegate to build. The extensions take the specific Action delegate, so the compiler just converts the method group to that.

people

See more on this question at Stackoverflow