Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created February 11, 2011 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeminutillo/821846 to your computer and use it in GitHub Desktop.
Save mikeminutillo/821846 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
namespace MagicUI.Framework
{
public static class Execute
{
[ThreadStatic]
private static bool _isUiThread;
private static Action<Action> _onUiExec = action => action();
public static void OnUi(Action action)
{
_onUiExec(action);
}
public static void SetUiContext(SynchronizationContext synchronizationContext)
{
_onUiExec = action =>
{
if (_isUiThread)
action();
else
synchronizationContext.Send(e => action(), null);
};
OnUi(() => _isUiThread = true);
}
}
}
using System.ComponentModel;
namespace MagicUI.Framework
{
public class NotifiesOfPropertyChangeOnUiThread : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected virtual void NotifyPropertyChanged(string propertyName)
{
Execute.OnUi(() => PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));
}
}
}
public void Run()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var rootWindow = new Window();
// NOTE: Must be after the first form is created
Execute.SetUiContext(SynchronizationContext.Current);
var rootViewModel = CreateRootViewModel();
rootWindow.SetViewModel(rootViewModel);
Application.Run(rootWindow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment