public static void Execute(string title, Action <Action <long>, Action <long> > action) { using (UiProgressWindow window = new UiProgressWindow(title)) { Task.Run(() => ExecuteAction(window, action)); window.ShowDialog(); } }
public static T Execute <T>(string title, Func <Action <long>, Action <long>, T> action) { using (UiProgressWindow window = new UiProgressWindow(title)) { Task <T> task = Task.Run(() => ExecuteFunction(window, action)); window.ShowDialog(); return(task.Result); } }
public static void Execute(string title, IProgressSender progressSender, Action action) { using (UiProgressWindow window = new UiProgressWindow(title)) { progressSender.ProgressTotalChanged += window.SetTotal; progressSender.ProgressIncremented += window.Incremented; Task.Run(() => ExecuteAction(window, action)); window.ShowDialog(); } }
private static T ExecuteFunction <T>(UiProgressWindow window, Func <Action <long>, Action <long>, T> action) { try { return(action(window.SetTotal, window.Incremented)); } finally { window.Dispatcher.Invoke(window.Close); } }
private static T ExecuteFunction <T>(UiProgressWindow window, Func <T> func) { try { return(func()); } finally { window.Dispatcher.Invoke(window.Close); } }
private static void ExecuteAction(UiProgressWindow window, Action <Action <long>, Action <long> > action) { try { action(window.SetTotal, window.Incremented); } finally { window.Dispatcher.Invoke(window.Close); } }
private static void ExecuteAction(UiProgressWindow window, Action action) { try { action(); } finally { window.Dispatcher.Invoke(window.Close); } }
public static T Execute <T>(string title, IProgressSender progressSender, Func <T> func) { using (UiProgressWindow window = new UiProgressWindow(title)) { progressSender.ProgressTotalChanged += window.SetTotal; progressSender.ProgressIncremented += window.Incremented; Task <T> task = Task.Run(() => ExecuteFunction(window, func)); window.ShowDialog(); return(task.Result); } }
internal static async Task <Boolean> CheckUpdates(Window rootElement, ManualResetEvent cancelEvent, GameSettingsControl gameSettings) { String applicationPath = Path.GetFullPath(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)); String applicationDirectory = Path.GetDirectoryName(applicationPath); LinkedList <HttpFileInfo> updateInfo = await FindUpdatesInfo(applicationDirectory, cancelEvent, gameSettings); if (updateInfo.Count == 0) { return(false); } StringBuilder messageSb = new StringBuilder(256); messageSb.AppendLine(Lang.Message.Question.NewVersionIsAvailable); Int64 size = 0; foreach (HttpFileInfo info in updateInfo) { size += info.ContentLength; messageSb.AppendLine($"{info.TargetName} - {info.LastModified} ({UiProgressWindow.FormatValue(info.ContentLength)})"); } if (MessageBox.Show(rootElement, messageSb.ToString(), Lang.Message.Question.Title, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { List <String> success = new List <String>(updateInfo.Count); List <String> failed = new List <String>(); using (UiProgressWindow progress = new UiProgressWindow("Downloading...")) { progress.SetTotal(size); progress.Show(); Downloader downloader = new Downloader(cancelEvent); downloader.DownloadProgress += progress.Incremented; foreach (HttpFileInfo info in updateInfo) { String filePath = info.TargetPath; try { await downloader.Download(info.Url, filePath); File.SetLastWriteTime(filePath, info.LastModified); success.Add(filePath); } catch { failed.Add(filePath); } } } if (failed.Count > 0) { MessageBox.Show(rootElement, "Failed to download:" + Environment.NewLine + String.Join(Environment.NewLine, failed), Lang.Message.Error.Title, MessageBoxButton.OK, MessageBoxImage.Error); } if (success.Count > 0) { String main = success.First(); if (success.Count > 1) { StringBuilder sb = new StringBuilder(256); foreach (String path in success.Skip(1)) { sb.Append('"'); sb.Append(path); sb.Append('"'); } Process.Start(main, $@"-update ""{applicationPath}"" ""{Process.GetCurrentProcess().Id}"" {sb}"); } else { Process.Start(main, $@"-update ""{applicationPath}"" ""{Process.GetCurrentProcess().Id}"""); } Environment.Exit(2); return(true); } else { return(false); } } return(false); }