0

Is there any way to dispatch all methods in class in easy way?

For example my class is a adapter for UI items and contains static methods. So instead of dispatching all methods separately in class, it could be done like this: Who ever is calling a method from this class, everything is dispatched through UI thread.

Like from:

public MyClass
{
   ...

   public static void Method1
   {
      Application.Current.Dispatcher.Invoke(() =>
      {
          /* do something */
      }
   }

   public static bool Method2
   {
      return Application.Current.Dispatcher.Invoke(() =>
      {
          return UIitem.SomeProperty;
      }
   }
}

To this:

[DispatcherAttribute] /* or something similar */
public MyClass
{
   ...

   public static void Method1
   {
      /* do something */
   }

   public static bool Method2
   {
      return UIitem.SomeProperty;
   }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
RassK
  • 515
  • 6
  • 20
  • 2
    What you're trying to do can be achieved with the various AOP (aspect oriented programming) frameworks. Have a look at [Linfu](https://code.google.com/p/linfu/) – Yuval Itzchakov Dec 24 '14 at 13:11
  • 1
    @Yuval not for static methods, unfortunately... Maybe PostSharp could help in this situation. – Lucas Trzesniewski Dec 24 '14 at 13:20
  • 1
    I know that Fody's plugin - PropertyChanged allows generating `PropertyChanged` events using the `Dispatcher`, [please see here](https://github.com/Fody/PropertyChanged/wiki/NotificationInterception) . So might be you can make some research in that direction. – ie. Dec 24 '14 at 13:21
  • 1
    @LucasTrzesniewski just noticed the static methods myself only after your comment haha. Static methods usually is a code smell, he could always move that into a singleton pattern (still a code smell) – Michal Ciechan Dec 24 '14 at 13:24
  • @RassK Are your methods `static`? or is it just because you generated this using ConsoleApplication1? – Yuval Itzchakov Dec 24 '14 at 13:25
  • They are static but could be easily non-static using Singleton pattern. Additional framework will be way too much for just this class. I'm already using PRISM if there is something that helps? Application itself is in WPF. – RassK Dec 24 '14 at 13:48
  • I have closed your question as a duplicate. Please have a look to the linked answer in order to understand how to properly operate with WPF UI elements in a multi threaded scenario. Other than that, feel free to ask a new question if that answer does not satisy your needs. – Federico Berasategui Dec 24 '14 at 17:14
  • @HighCore, there is absolutely nothing connected with the linked answer. Personally can't see connection at all. – RassK Dec 24 '14 at 19:02
  • @Rassk It seems to me like so, because instead of using proper DataBinding you're somehow attempting to manipulate UI elements' properties from a background thread, which is not feasible in WPF. The linked answer mentions a way to have property change notification automatically dispatched to the UI thread using the `INotifyPropertyChanged` interface. Whatever hacks you might want to come up to do otherwise are simply not useful in WPF. – Federico Berasategui Dec 24 '14 at 19:16
  • @RassK Also, there is no such thing as an `"adapter for UI items"` - nor anything like that in WPF. You seem to lack very basic understanding of how WPF works and are rather attempting to use WPF as if it were winforms or any other ancient framework, which it isn't. I suggest you take the time to carefully read my answer and the explanation about the *WPF Mentality* before you continue to do any development. – Federico Berasategui Dec 24 '14 at 19:20
  • @HighCore, i definitely know how to use MVVM with WPF, but for this special case using DataBinding just messes up your view models, there are way too many properties to bind to one control - specially when you have more than 1 (like 10) these controls in same view. We are using complicated custom UI controls with extension 'adapter' for some special cases. I tried to keep the question as simple as possible. There is no way to explain the whole idea here. So either there is a solution described before or not, that's the only usable answer atm for me. – RassK Dec 25 '14 at 07:23
  • @HighCore, i really appreciate your feedback and help. If you would like to dig deeper to problem, please, lets use more efficient ways to communicate. – RassK Dec 25 '14 at 07:25

0 Answers0