• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjMw

Related Code
Strongly typed NotifyPropertyChanged
Invoke Extension Method
Fork of Strongly typed NotifyPropertyChanged

Execute.OnUIThread & Execute.InBackground utility methods

Helper methods to run delegates on ui thread or on background thread

2
831 0 1 0 0 0

Usage:

Execute.OnUIThread(() => PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));

And:

Execute.InBackground(() => SaveModel(),
            exception =>
            {
				//HandleError()
            });

Execute.InBackground(() => GetModel(),
            (model, exception) =>
            {
				//HandleError & do something with model
            });

The best thing about abstracting these to methods is that you can change behavior for these for unit tests (so that both run on the current thread).

Implementation:

public static class Execute
{
	private static Action<Action> _uiThreadExecutor = action => action();
	private static Action<Action> _backgroundExecutor = action => action();

	public static void InitializeWithDispatcher()
	{
		var dispatcher = Dispatcher.CurrentDispatcher;

		_uiThreadExecutor = action =>
		{
		   if (dispatcher.CheckAccess())
                action();
            else 
                dispatcher.BeginInvoke(action);
		};

		_backgroundExecutor = action => ThreadPool.QueueUserWorkItem(obj => action());
	}

	public static void InBackground<TResult>(this Func<TResult> action, Action<TResult, Exception> callback)
	{
			_backgroundExecutor(() =>
			{
					Exception exception = null;
					TResult result = default(TResult);
					try
					{
						result = action();
					}
					catch (Exception ex)
					{
						exception = ex;
					}

					OnUIThread(() => callback(result, exception));
			});
	}

	public static void InBackground(this Action action, Action<Exception> callback)
	{
			_backgroundExecutor(() =>
			{
				Exception exception = null;
				try
				{
					action();
				}
				catch (Exception ex)
				{
				    exception = ex;
				}

				OnUIThread(() => callback(exception));
			});
	}

	public static void OnUIThread(this Action action)
	{
		_uiThreadExecutor(action);
	}
}

As you can see the default delegates just execute the action directly, resulting in single thread behavior for unit tests. The InitializeWithDispatcher method is only run from the bootstrapper of the application (which is not run from unit tests).


Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

threading

Mark 'threading' tag as 'like'

Mark 'threading' tag as 'ignore'

wpf

Mark 'wpf' tag as 'like'

Mark 'wpf' tag as 'ignore'


 @torkelo
216
August 16, 2010 3:57 PM
edited August 16, 2010 3:59 PM

Fork

 Execute.OnUIThread & Execute.InBackground utility methods -  @torkelo Monday 16, 2010 3:57 PM


1 Feedback

Cool - that's more or less the same thing I do. Using the dispatcher directly is pretty gross. -  cstrahan Thursday 16, 2010 8:41 PM

You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010