private async void doWork(string message, CustomEvent.EventType type) { bool result = await doNotificationAsync(message, type); }
private Task <bool> doNotificationAsync(string message, CustomEvent.EventType type) { SolidColorBrush critical = new SolidColorBrush(Color.FromArgb(0xFF, Convert.ToByte(220), Convert.ToByte(53), Convert.ToByte(69))); SolidColorBrush warning = new SolidColorBrush(Color.FromArgb(0xFF, Convert.ToByte(255), Convert.ToByte(193), Convert.ToByte(7))); SolidColorBrush accept = new SolidColorBrush(Color.FromArgb(0xFF, Convert.ToByte(40), Convert.ToByte(167), Convert.ToByte(69))); //SolidColorBrush backgroundBrush = new SolidColorBrush(Color.FromArgb(0xFF, Convert.ToByte(230), Convert.ToByte(231), Convert.ToByte(237))); TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); object locker = new object(); object locker2 = new object(); try { Task.Run(() => { lock (locker) { Application.Current.Dispatcher.Invoke((Action) delegate { lock (locker2) { System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Interval = new TimeSpan(0, 0, 3); dispatcherTimer.Tick += (sernder, e) => { ViewModel.NotificationEnabled = false; }; dispatcherTimer.Start(); switch (type) { case CustomEvent.EventType.warning: { notificationGrid.Background = warning; tbNotifyMessage.Text = message; ViewModel.NotificationEnabled = true; } break; case CustomEvent.EventType.critical: { notificationGrid.Background = critical; tbNotifyMessage.Text = message; ViewModel.NotificationEnabled = true; } break; case CustomEvent.EventType.accept: { notificationGrid.Background = accept; tbNotifyMessage.Text = message; ViewModel.NotificationEnabled = true; } break; default: break; } } }); } tcs.SetResult(true); }); // set a timmer for how long the notification should be visible // return true when timer is done. return(tcs.Task); } catch (Exception exception) { ErrorHandler.ErrorHandle error = ErrorHandler.ErrorHandle.getInstance(); error.handle(exception, true); } return(tcs.Task); }