winfom实现关闭后一直运行
Posted daimaxuejia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winfom实现关闭后一直运行相关的知识,希望对你有一定的参考价值。
using PLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace DCZY.CVServer { static class Program { public static System.Threading.Mutex Run; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.ApplicationExit += new EventHandler(Application_Exit); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); PLog.LogComm.System_Path = @"."; Log.WriteDebug("启动!"); Thread.Sleep(2000); bool noRun = false; Run = new System.Threading.Mutex(true, "DCZY.CVServer", out noRun); //检测是否已经运行 if (noRun) { Run.ReleaseMutex(); Application.Run(new FrmCVServer()); } else { Thread.Sleep(2000); Run = new System.Threading.Mutex(true, "DCZY.CVServer", out noRun); if (!noRun) { //已经运行 MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); Process process = RuningInstance(); if (process != null) { HandleRunningInstance(process); } } } } private static void HandleRunningInstance(Process instance) { ShowWindowAsync(instance.MainWindowHandle, 1);//显示 SetForegroundWindow(instance.MainWindowHandle);//当到最前端 } #region Windows API定义 /// 该函数设置由不同线程产生的窗口的显示状态 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分</param> /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零</returns> [DllImport("User32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// <summary> /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。 /// 系统给创建前台窗口的线程分配的权限稍高于其他线程。 /// </summary> /// <param name="hWnd">将被激活并被调入前台的窗口句柄</param> /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零</returns> [DllImport("User32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); #endregion private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { try { Log.WriteError(e.Exception.StackTrace); Application.Restart(); } catch { } } private static Process RuningInstance() { Process currentProcess = Process.GetCurrentProcess(); Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName); foreach (Process process in Processes) { if (process.Id != currentProcess.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName) { return process; } } } return null; } static void Application_Exit(object sender, EventArgs e) { Log.WriteInfo("系统已经退出"); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { try { Log.WriteError((e.ExceptionObject as Exception).TargetSite + (e.ExceptionObject as Exception).Message); Log.WriteError((e.ExceptionObject as Exception).Message + Environment.NewLine + (e.ExceptionObject as Exception).Source + Environment.NewLine + (e.ExceptionObject as Exception).StackTrace); } catch { } } } }
以上是关于winfom实现关闭后一直运行的主要内容,如果未能解决你的问题,请参考以下文章