|
| 1 | +using AutoHDR.ProjectResources; |
| 2 | +using CodectoryCore.Logging; |
| 3 | +using CodectoryCore.UI.Wpf; |
| 4 | +using Microsoft.Win32; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Drawing; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Runtime.Serialization; |
| 13 | +using System.Text; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +namespace AutoHDR.Profiles.Actions |
| 17 | +{ |
| 18 | + [JsonObject(MemberSerialization.OptIn)] |
| 19 | + public class CloseProgramAction : ProfileActionBase |
| 20 | + { |
| 21 | + public override string ActionTypeName => ProjectResources.Locale_Texts.CloseProgram; |
| 22 | + |
| 23 | + |
| 24 | + private string _processName = ""; |
| 25 | + |
| 26 | + [JsonProperty] |
| 27 | + public string ProcessName { get => _processName; set { _processName = value; OnPropertyChanged(); } } |
| 28 | + |
| 29 | + private bool _force = false; |
| 30 | + |
| 31 | + [JsonProperty] |
| 32 | + public bool Force { get => _force; set { _force = value; OnPropertyChanged(); } } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + public override string ActionDescription => $"{Locale_Texts.Close} {ProcessName}"; |
| 37 | + |
| 38 | + public RelayCommand GetFileCommand { get; private set; } |
| 39 | + |
| 40 | + |
| 41 | + public CloseProgramAction() |
| 42 | + { |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + public override ActionEndResult RunAction(params object[] parameter) |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + |
| 51 | + Process[] runningProcesses = Process.GetProcesses(); |
| 52 | + foreach (Process process in runningProcesses) |
| 53 | + { |
| 54 | + if (process.ProcessName == ProcessName) |
| 55 | + try |
| 56 | + { |
| 57 | + CallNewLog(new LogEntry($"Closing {ProcessName}...")); |
| 58 | + |
| 59 | + bool result = process.CloseMainWindow(); |
| 60 | + if (!result) |
| 61 | + |
| 62 | + process.Close(); |
| 63 | + } |
| 64 | + catch (Exception ex) |
| 65 | + { |
| 66 | + throw new Exception($"Closing {ProcessName} failed", ex); |
| 67 | + } |
| 68 | + if (!process.HasExited) |
| 69 | + { |
| 70 | + if (Force) |
| 71 | + { |
| 72 | + CallNewLog(new LogEntry($"Killing {ProcessName}...")); |
| 73 | + try |
| 74 | + { |
| 75 | + process.Kill(); |
| 76 | + CallNewLog(new LogEntry($"Process {ProcessName} killed..")); |
| 77 | + |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + throw new Exception($"Killing {ProcessName} failed", ex); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + else |
| 86 | + CallNewLog(new LogEntry($"Process {ProcessName} closed.")); |
| 87 | + |
| 88 | + |
| 89 | + } |
| 90 | + return new ActionEndResult(true); |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + CallNewLog(new CodectoryCore.Logging.LogEntry($"{ ex.Message }\r\n{ ex.StackTrace}", CodectoryCore.Logging.LogEntryType.Error)); |
| 95 | + return new ActionEndResult(false, ex.Message, ex); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments