From 2d69560029bc791765b0e721f7c810a29c70600c Mon Sep 17 00:00:00 2001 From: Labo89 Date: Thu, 18 Feb 2021 19:46:40 +0100 Subject: [PATCH] Refacoring, removed unused code, changed version --- adbGUI/Forms/BackupRestore.cs | 2 +- adbGUI/Forms/InstallUninstall.cs | 2 +- adbGUI/Forms/LogcatAdvanced.cs | 2 +- adbGUI/Forms/MainForm.cs | 6 +-- adbGUI/Forms/ScreenRecord.cs | 2 +- adbGUI/Methods/CLI.cs | 81 +++++++++++++++++++++---------- adbGUI/Methods/Dependencies.cs | 12 ++--- adbGUI/Methods/DevicesWatcher.cs | 2 +- adbGUI/Methods/HelperClass.cs | 8 +-- adbGUI/Properties/AssemblyInfo.cs | 6 +-- 10 files changed, 73 insertions(+), 50 deletions(-) diff --git a/adbGUI/Forms/BackupRestore.cs b/adbGUI/Forms/BackupRestore.cs index e64cbf1..7059ecb 100644 --- a/adbGUI/Forms/BackupRestore.cs +++ b/adbGUI/Forms/BackupRestore.cs @@ -81,7 +81,7 @@ private void Cbo_BackupPackage_CheckedChanged(object sender, EventArgs e) groupBox8.Enabled = false; groupBox14.Enabled = false; - var output = HelperClass.ExecuteWithOutput("adb shell pm list packages -3"); + var output = HelperClass.ExecuteWithOutput("adb", "shell pm list packages -3"); if (!string.IsNullOrEmpty(output)) { diff --git a/adbGUI/Forms/InstallUninstall.cs b/adbGUI/Forms/InstallUninstall.cs index 250c5f9..9f78078 100644 --- a/adbGUI/Forms/InstallUninstall.cs +++ b/adbGUI/Forms/InstallUninstall.cs @@ -60,7 +60,7 @@ private void RefreshInstalledApps() cbx_InstallUninstallPackageUninstall.Enabled = false; - var output = HelperClass.ExecuteWithOutput("adb shell pm list packages -3"); + var output = HelperClass.ExecuteWithOutput("adb", "shell pm list packages -3"); if (!string.IsNullOrEmpty(output)) { diff --git a/adbGUI/Forms/LogcatAdvanced.cs b/adbGUI/Forms/LogcatAdvanced.cs index 9fed24d..55644bb 100644 --- a/adbGUI/Forms/LogcatAdvanced.cs +++ b/adbGUI/Forms/LogcatAdvanced.cs @@ -43,7 +43,7 @@ private void Btn_LogcatAdvancedClearBuffers_Click(object sender, EventArgs e) private void Btn_LogcatAdvancedStop_Click(object sender, EventArgs e) { - CLI.AbortChildProcessesAsync(); + CLI.KillChildProcessesAsync(); } private void Btn_LogcatAdvancedNewBufferSize_Click(object sender, EventArgs e) diff --git a/adbGUI/Forms/MainForm.cs b/adbGUI/Forms/MainForm.cs index 1d9d8d3..0b6ce43 100644 --- a/adbGUI/Forms/MainForm.cs +++ b/adbGUI/Forms/MainForm.cs @@ -60,7 +60,7 @@ private void Btn_ConsoleClear_Click(object sender, EventArgs e) private void Btn_consoleStop_Click(object sender, EventArgs e) { - CLI.AbortChildProcessesAsync(); + CLI.KillChildProcessesAsync(); } private void Btn_executeCommand_Click(object sender, EventArgs e) @@ -83,7 +83,7 @@ private void CloseApplication() { Debug.WriteLine("Shutting down..."); - Debug.WriteLine(" > Killing child processes..."); + Debug.WriteLine(" > Killing all child processes..."); CLI.StopWithShell(); Debug.WriteLine(" > Closing application..."); @@ -109,7 +109,7 @@ private void MainForm_KeyDown(object sender, KeyEventArgs e) if ((!cbx_customCommand.Focused || String.IsNullOrWhiteSpace(cbx_customCommand.SelectedText)) && String.IsNullOrWhiteSpace(rtb_console.SelectedText)) { Debug.WriteLine("Keypress detected: CTRL + C"); - CLI.AbortChildProcessesAsync(); + CLI.KillChildProcessesAsync(); e.Handled = e.SuppressKeyPress = true; } diff --git a/adbGUI/Forms/ScreenRecord.cs b/adbGUI/Forms/ScreenRecord.cs index 537afc2..7106f48 100644 --- a/adbGUI/Forms/ScreenRecord.cs +++ b/adbGUI/Forms/ScreenRecord.cs @@ -68,7 +68,7 @@ private void Btn_SreenRecordAbort_Click(object sender, EventArgs e) { timer.Enabled = false; btn_screenRecordStart.Text = @"Start"; - CLI.AbortChildProcesses(); + CLI.KillChildProcesses(); } private void Timer_Tick(object sender, EventArgs e) diff --git a/adbGUI/Methods/CLI.cs b/adbGUI/Methods/CLI.cs index 90cfa3c..af8b97f 100644 --- a/adbGUI/Methods/CLI.cs +++ b/adbGUI/Methods/CLI.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Management; @@ -38,38 +39,71 @@ static CLI() public static Process Commandline { get; set; } = new Process(); - public static void AbortChildProcesses() + public static List GetChildProcesses() { + List lst = new List(); + using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ParentProcessID=" + Commandline.Id + " AND Caption != 'conhost.exe'")) { ManagementObjectCollection managementObjectCollection = searcher.Get(); foreach (ManagementObject managementObject in managementObjectCollection) { - int pid = Convert.ToInt32(managementObject["ProcessID"]); - Debug.WriteLine($"Killing {managementObject["Caption"]}({managementObject["ProcessID"]})"); - Process.GetProcessById(pid).Kill(); + lst.Add(Convert.ToInt32(managementObject["ProcessID"])); } } + + return lst; } - public static void AbortChildProcessesAsync() + public static void KillChildProcesses() { - Task.Run(() => { AbortChildProcesses(); }); + foreach (int pid in GetChildProcesses()) + { + Debug.WriteLine($"Killing {pid})"); + Process.GetProcessById(pid).Kill(); + } } - public static void AbortChildProcessesWithShell() + public static void KillChildProcessesAsync() + { + Task.Run(() => { KillChildProcesses(); }); + } + + public static void KillChildProcessesWithShell() { string input = "taskkill /F "; - using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ParentProcessID=" + Commandline.Id + " AND Caption != 'conhost.exe'")) + foreach (int pid in GetChildProcesses()) { - ManagementObjectCollection managementObjectCollection = searcher.Get(); + input += $"/PID {pid} "; + } - foreach (ManagementObject managementObject in managementObjectCollection) - { - int pid = Convert.ToInt32(managementObject["ProcessID"]); - input += $"/PID {pid} "; - } + if (input == "taskkill /F ") return; + + Process cmd = new Process(); + + ProcessStartInfo startInfo = new ProcessStartInfo() + { + FileName = "cmd", + Arguments = "/c " + input, + UseShellExecute = false, + CreateNoWindow = true, + }; + + cmd.StartInfo = startInfo; + + Debug.WriteLine("Executing: cmd /c " + input); + + cmd.Start(); + } + + public static void KillAllAdbProcessesWithShell() + { + string input = "taskkill /F "; + + foreach (Process process in Process.GetProcessesByName("adb")) + { + input += $"/PID {process.Id} "; } if (input == "taskkill /F ") return; @@ -86,9 +120,10 @@ public static void AbortChildProcessesWithShell() cmd.StartInfo = startInfo; - Debug.WriteLine("Executing: cmd /c" + input); + Debug.WriteLine("Executing: cmd /c " + input); cmd.Start(); + } public static void Execute(string command) @@ -96,14 +131,14 @@ public static void Execute(string command) Commandline.StandardInput.WriteLine(command); } - public static string GetOutput(string command) + public static string GetOutput(string fileName, string arguments) { Process cmd = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo() { - FileName = "cmd", - Arguments = "/c " + command, + FileName = fileName, + Arguments = arguments, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, @@ -122,15 +157,9 @@ public static string GetOutput(string command) return cmd.StandardOutput.ReadToEnd(); } - internal static void Stop() - { - AbortChildProcesses(); - Commandline.Kill(); - } - - internal static void StopWithShell() + public static void StopWithShell() { - AbortChildProcessesWithShell(); + KillChildProcessesWithShell(); Commandline.Kill(); } } diff --git a/adbGUI/Methods/Dependencies.cs b/adbGUI/Methods/Dependencies.cs index a8b96c9..6c727cf 100644 --- a/adbGUI/Methods/Dependencies.cs +++ b/adbGUI/Methods/Dependencies.cs @@ -38,7 +38,10 @@ public static void Check() { case DialogResult.Yes: DownloadFiles(); + ExtractFiles(); + MessageBox.Show(@"Files downloaded", @"Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); break; + default: Environment.Exit(0); break; @@ -64,20 +67,11 @@ private static void DownloadFiles() { wc.DownloadFile(downloadUri, downloadedZipFile); } - - ExtractFiles(); } private static void ExtractFiles() { - if (Directory.Exists(tmpPlatformPath)) - { - Directory.Delete(tmpPlatformPath, true); - } - ZipFile.ExtractToDirectory(downloadedZipFile, Path.GetTempPath()); - - MessageBox.Show(@"Files downloaded", @"Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } private static void SetEnvVariable() diff --git a/adbGUI/Methods/DevicesWatcher.cs b/adbGUI/Methods/DevicesWatcher.cs index 95d03a6..5661e70 100644 --- a/adbGUI/Methods/DevicesWatcher.cs +++ b/adbGUI/Methods/DevicesWatcher.cs @@ -27,7 +27,7 @@ public static void Refresh() { while (true) { - string output = CLI.GetOutput("adb devices"); + string output = CLI.GetOutput("adb", "devices"); if (output != oldOutput) { diff --git a/adbGUI/Methods/HelperClass.cs b/adbGUI/Methods/HelperClass.cs index 09b2542..0e586f8 100644 --- a/adbGUI/Methods/HelperClass.cs +++ b/adbGUI/Methods/HelperClass.cs @@ -34,16 +34,16 @@ public static void Execute(string command, bool withSerial = true) } } - public static string ExecuteWithOutput(string command, bool withSerial = true) + public static string ExecuteWithOutput(string fileName, string arguments, bool withSerial = true) { if (String.IsNullOrWhiteSpace(SelectedDevice) || withSerial == false) { - return CLI.GetOutput(command); + return CLI.GetOutput(fileName, arguments); } else { - string cmd = command.Replace("adb ", "adb -s " + SelectedDevice + " "); - return CLI.GetOutput(cmd); + arguments = "-s " + SelectedDevice + " " + arguments; + return CLI.GetOutput(fileName, arguments); } } diff --git a/adbGUI/Properties/AssemblyInfo.cs b/adbGUI/Properties/AssemblyInfo.cs index 5db4d39..cedca7f 100644 --- a/adbGUI/Properties/AssemblyInfo.cs +++ b/adbGUI/Properties/AssemblyInfo.cs @@ -12,7 +12,7 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Labo")] [assembly: AssemblyProduct("adbGUI")] -[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyCopyright("Copyright © 2021")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -34,5 +34,5 @@ // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.1.0.1")] -[assembly: AssemblyFileVersion("2.1.0.0")] +[assembly: AssemblyVersion("2.2.0.0")] +[assembly: AssemblyFileVersion("2.2.0.0")]