private static void ShowWaitWindow() { shownLock.WaitOne(); if (taskCompleted) { shownLock.ReleaseMutex(); return; } window = new WaitWindow(); window.Owner = Application.Current.MainWindow; window.Loaded += (sender, e) => { shownLock.ReleaseMutex(); }; Item item = null; lock (queueLock) { item = queue.Peek(); } UpdateUI(item.Title, item.Text, null); window.ShowDialog(); }
private static void HideWaitWindow() { shownLock.WaitOne(); taskCompleted = true; if (window != null) { window.Hide(); window = null; } shownLock.ReleaseMutex(); }
public static void DoWork(Func <object, object> action, Action <object> completed, object state, string title, string text) { Application.Current.MainWindow.Cursor = Cursors.Wait; ((UIElement)(Application.Current.MainWindow.Content)).IsEnabled = false; lock (queueLock) { queue.Enqueue(new Item() { Action = action, Completed = completed, State = state, Title = title, Text = text, }); if (queue.Count > 1) { return; } finishedTasks = 0; } shownLock.WaitOne(); window = null; taskCompleted = false; shownLock.ReleaseMutex(); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (s, e) => { bool empty = false; do { Item item = null; double?progress = null; lock (queueLock) { if (queue.Count <= 0) { break; } item = queue.Peek(); if (!(queue.Count == 1 && finishedTasks == 0)) { progress = 1.0 * (finishedTasks + 1) / (queue.Count + finishedTasks); } } Application.Current.Dispatcher.BeginInvoke((Action <string, string, double?>) UpdateUI, item.Title, item.Text, progress); var result = item.Action(item.State); Application.Current.Dispatcher.BeginInvoke(item.Completed, result); lock (queueLock) { queue.Dequeue(); empty = (queue.Count <= 0); finishedTasks++; } }while (!empty); }; worker.RunWorkerCompleted += (s, e) => { Application.Current.Dispatcher.BeginInvoke((Action)HideWaitWindow); Application.Current.MainWindow.Cursor = Cursors.Arrow; ((UIElement)(Application.Current.MainWindow.Content)).IsEnabled = true; }; worker.RunWorkerAsync(); BackgroundWorker timer = new BackgroundWorker(); timer.DoWork += (s, e) => { Thread.Sleep(TimeSpan.FromSeconds(1)); shownLock.WaitOne(); Application.Current.Dispatcher.BeginInvoke((Action)ShowWaitWindow); shownLock.ReleaseMutex(); }; timer.RunWorkerAsync(); }