Showing posts with label delegate. Show all posts
Showing posts with label delegate. Show all posts

2013-08-29

C#: Crosscontext calls in WPF

public static class DispatcherObjectExtensions{
 public static TRet myInvoke(this T source, Func func)  where T:DispatcherObject {
  return (TRet)source.Dispatcher.Invoke(func);
 }
 public static void myInvoke(this T source, Action act) where T:DispatcherObject {
  source.Dispatcher.Invoke(act);
 }
}

This will put these two extension methods on all DispatcherObject descendants (like System.Windows.Window), and you can call a delegate put in the object's thread/context.

C#: Anonymous delegates asynchronously

this.BeginInvoke(new Action(delegate
{ 
 MessageBox.Show("Asynchronous :D"); 
}));

System.Action is a framework-defined Delegate successor, that does not return anything and doesn't take any arguments. You should use this, instead of fucking around with inheriting from System.Delegate.