/// <summary>
/// Invokes the specified action on the UI thread.
/// </summary>
/// <param name="action">The action.</param>
protected void Invoke(Action action)
{
System.Windows.Application.Current.Dispatcher.Invoke(action);
}
/// <summary>
/// Invokes it if needed on the main thread else uses the current thread
/// </summary>
/// <param name="action"></param>
protected void InvokeIfRequired(Action action)
{
if (InvokeRequired())
Invoke(action);
else
action.Invoke();
}
/// <summary>
/// Used to test if the action is on a background thread
/// </summary>
/// <returns></returns>
protected bool InvokeRequired() => System.Windows.Application.Current?.Dispatcher?.Thread is not null && Thread.CurrentThread != System.Windows.Application.Current.Dispatcher.Thread;
1条答案
按热度按时间knsnq2tg1#
字符串
这将工作:-)
我的视图模型基类中有这个
型
我发现它使用起来很简单,尤其是对于所有的异步任务和await
这样我就不用跟踪了