Created
August 24, 2014 17:26
-
-
Save AudriusButkevicius/ae7b2a818ce4d7f6213c to your computer and use it in GitHub Desktop.
MahApps.Metro C# UI blocking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainWindow : MetroWindow | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
RegisterHandlers(); | |
this.Loaded += WindowLoaded; | |
} | |
void WindowLoaded(object sender, RoutedEventArgs e) | |
{ | |
SynchronizeSyncthing(); | |
} | |
async private void SynchronizeSyncthing() | |
{ | |
if (Process.GetProcessesByName("syncthing").Length > 0) | |
{ | |
var result = await this.ShowMessageAsync("Oops", "Syncthing seems to be already running.\nWould you like to kill the existing instance to continue?", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings | |
{ | |
AffirmativeButtonText = "Kill", | |
NegativeButtonText = "Exit", | |
}); | |
if (result == MessageDialogResult.Affirmative) { | |
var progressCtrl = await this.ShowProgressAsync("Bare with me", "Killing existing syncthing processes"); | |
foreach (int attempt in Enumerable.Range(1, 5)) | |
{ | |
if (attempt == 4) | |
{ | |
progressCtrl.CloseAsync(); | |
await this.ShowMessageAsync("Oops", "Can't kill existing Syncthing processes, please do it via Task Manager, and try again.", MessageDialogStyle.Affirmative); | |
Application.Current.Shutdown(); | |
} | |
var processes = Process.GetProcessesByName("syncthing"); | |
if (processes.Length == 0) | |
{ | |
progressCtrl.CloseAsync(); | |
break; | |
} | |
foreach (Process proc in processes) | |
{ | |
proc.Kill(); | |
proc.WaitForExit(2000); | |
proc.Refresh(); | |
} | |
} | |
} else { | |
Application.Current.Shutdown(); | |
} | |
} | |
StartSyncthing(); | |
} | |
private void StartSyncthing() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment