我有一个 Dictionary WPF 应用程序,我用热键 (Alt + A) 将它链接起来,这个应用程序在其 window_activated 事件中检查剪贴板中复制的文本并立即翻译它。
现在,当用户点击热键(Alt + A)时,会创建一个新的应用程序实例,所以我决定将其限制为一个实例,我设法做到了,但问题就在这里
用户点击(Alt + A)新实例被中止但先前存在的实例正在运行但我无法激活要激活的窗口这里是我编写的代码
/// <summary> /// Application Entry Point. /// </summary> [STAThread] [DebuggerNonUserCode] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] public static void Main() { // Get Reference to the current Process var thisProc = Process.GetCurrentProcess(); // Check how many total processes have the same name as the current one if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1) { // the new application instance. Application.Current.Shutdown(); foreach (var process in Process.GetProcessesByName(thisProc.ProcessName)) { if(process.Id != thisProc.Id) { // here i need to activate the main window of my one instance app. } } return; } var app = new App(); app.InitializeComponent(); app.Run(); }
任何帮助将不胜感激。