Chris McKelt

Remembering Thoughts

 

Recent comments

Archive

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2013

WPF UI Thread Dispatcher

A simple implemention for calling asych methods from the UI

 

Examples

 

1.

dispatcher.ExecuteOnMainUIThread(CommandManager.InvalidateRequerySuggested);

 

2.

dispatcher.Execute(() =>
{
SomeLongRunningMethodHere();
});

 

The interface

 
using System;

namespace Mvvm
{
public interface IDispatcher
{
void Execute(Action action);

void ExecuteOnMainUIThread(Action action);
}
}

Synchronous for use in Testing

 
using System;

namespace Mvvm
{
public class SynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action();
}

public void ExecuteOnMainUIThread(Action action)
{
action();
}
}

}

Asynchronous for use by the application at run time

 
using System;

namespace Mvvm
{
using System.Windows;
using System.Windows.Threading;

public class AsynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action.BeginInvoke(CallBack, action);
}

public void ExecuteOnMainUIThread(Action action)
{
Dispatcher dispatcher;

if (Application.Current != null)
{
dispatcher = Application.Current.Dispatcher;
}
else
{
dispatcher = Dispatcher.CurrentDispatcher;
}

dispatcher.Invoke(action);
}

private void CallBack(IAsyncResult result)
{
try
{
((Action)result.AsyncState).EndInvoke(result);
}
catch (Exception ex)
{
// Need to raise the exception on the main thread
ExecuteOnMainUIThread(() =>
{
throw ex;
}
);
}
finally
{
result.AsyncWaitHandle.Close();
}


}
}
}


Posted by chris on Wednesday, May 26, 2010 3:15 AM
Permalink | Comments (1) | Post RSSRSS comment feed

Comments (1) -

używane komputery United States

Tuesday, October 26, 2010 12:29 PM

używane komputery

Do you accept guest posts? I would love to write couple articles here.