1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Diagnostics;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace prBrowserUpdate
- {
- internal static class Program
- {
- [STAThread]
- private static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(defaultValue: false);
- Process instance = RunningInstance();
- if (instance == null)
- {
- Application.Run(new MainForm());
- }
- else
- {
- HandleRunningInstance(instance);
- }
- }
- private static Process RunningInstance()
- {
- Process current = Process.GetCurrentProcess();
- Process[] processesByName = Process.GetProcessesByName(current.ProcessName);
- foreach (Process process in processesByName)
- {
- if (process.Id != current.Id && Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
- {
- return process;
- }
- }
- return null;
- }
- private static void HandleRunningInstance(Process instance)
- {
- ShowWindowAsync(instance.MainWindowHandle, 3);
- SetForegroundWindow(instance.MainWindowHandle);
- }
- [DllImport("User32.dll")]
- private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
- [DllImport("User32.dll")]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
- }
- }
|