Files
LaDOSE/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2019-03-09 14:03:25 +01:00
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
2019-03-09 14:03:25 +01:00
using System.Windows;
using System.Windows.Input;
2019-03-09 14:03:25 +01:00
namespace LaDOSE.DesktopApp.Utils
{
public static class WpfUtil
{
2019-03-13 00:59:40 +01:00
public static void AddUI<T>(this ICollection<T> collection, T item, Action action = null)
2019-03-09 14:03:25 +01:00
{
Action<T> addMethod = collection.Add;
Application.Current.Dispatcher.BeginInvoke(addMethod, item);
2019-03-13 00:59:40 +01:00
if(action!=null)
Application.Current.Dispatcher.BeginInvoke(action);
2019-03-09 14:03:25 +01:00
}
2019-03-13 00:59:40 +01:00
public static void Await(Action function,string message=null)
{
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
Task tsk = Task.Factory.StartNew(new Action(function));
tsk.ContinueWith(t =>
{
Application.Current.Dispatcher.Invoke(() =>
System.Windows.Input.Mouse.OverrideCursor = null);
MessageBox.Show(t.Exception.InnerException.Message);
},
CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
tsk.ContinueWith(task =>
{
if (!string.IsNullOrEmpty(message))
MessageBox.Show(message);
Application.Current.Dispatcher.Invoke(() =>
System.Windows.Input.Mouse.OverrideCursor = null);
});
}
2019-03-09 14:03:25 +01:00
}
}