Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Windows.Threading;
- namespace Client.AppUtils
- {
- /// <summary>
- /// Extentions class for Dispatcher
- /// </summary>
- public static class DispatcherExtentions
- {
- /// <summary>
- /// Returns true if the caller is on a separate thread than the UI thread
- /// </summary>
- public static bool IsInvokeRequired(this Dispatcher dispatcher)
- {
- return dispatcher.Thread.ManagedThreadId != Thread.CurrentThread.ManagedThreadId;
- }
- /// <summary>
- /// Calls the Dispatcher.Invoke() method if the caller is on a separate thread than the UI thread, otherwise the provided method is called
- /// </summary>
- public static void InvokeIfNeeded(this Dispatcher dispatcher, Action method, params object[] args)
- {
- if (IsInvokeRequired(dispatcher))
- dispatcher.Invoke(method, args);
- else
- method();
- }
- /// <summary>
- /// Calls the Dispatcher.Invoke() method if the caller is on a separate thread than the UI thread, otherwise the provided method is called
- /// </summary>
- public static T InvokeIfNeeded<T>(this Dispatcher dispatcher, Func<T> method)
- {
- if (IsInvokeRequired(dispatcher))
- return dispatcher.Invoke<T>(method);
- else
- return method();
- }
- /// <summary>
- /// Calls the Dispatcher.BeginInvoke() method if the caller is on a separate thread than the UI thread, otherwise the provided method is called
- /// </summary>
- public static void BeginInvokeIfNeeded(this Dispatcher dispatcher, Action action)
- {
- if (IsInvokeRequired(dispatcher))
- dispatcher.BeginInvoke(action);
- else
- action();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment