From 802b2daaaf29d20c41afb71fc38608348d1373fa Mon Sep 17 00:00:00 2001 From: Jakob Date: Sat, 9 Nov 2019 19:48:21 +0100 Subject: [PATCH] add check if application is running --- EnvyUpdate/MainWindow.xaml.cs | 8 ++++++-- EnvyUpdate/Util.cs | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index 99cd603..f0a9897 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -1,6 +1,5 @@ using System; using System.Windows; -using System.Windows.Shapes; using System.IO; using System.Net; using System.Text.RegularExpressions; @@ -26,6 +25,11 @@ namespace EnvyUpdate public MainWindow() { InitializeComponent(); + if (Util.IsProcessOpen("EnvyUpdate")) + { + MessageBox.Show("Application is already running."); + System.Environment.Exit(1); + } if (!Directory.Exists(appdata)) { Directory.CreateDirectory(appdata); @@ -39,7 +43,7 @@ namespace EnvyUpdate else { MessageBox.Show("No NVIDIA GPU found. Application will exit."); - System.Windows.Application.Current.Shutdown(); + System.Environment.Exit(255); } if (File.Exists(appdata + "nvidia-update.txt")) { diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 3854fd3..0fa59b8 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -1,12 +1,9 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management; -using System.Net; using System.IO; using IWshRuntimeLibrary; +using System.Diagnostics; namespace EnvyUpdate { @@ -86,5 +83,17 @@ namespace EnvyUpdate shortcut.TargetPath = targetFileLocation; shortcut.Save(); } + public static bool IsProcessOpen(string name) + { + foreach (Process clsProcess in Process.GetProcesses()) + { + if (clsProcess.ProcessName.Contains(name)) + { + return true; + } + } + + return false; + } } }