winfom实现关闭后一直运行

2021-06-05 23:03

阅读:453

标签:als   .sh   sage   message   end   win   sync   ted   efault   

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;
        /// 
        /// 应用程序的主入口点。
        /// 
        [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定义
        /// 该函数设置由不同线程产生的窗口的显示状态
        /// 
        /// 窗口句柄
        /// 指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分
        /// 如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零
        [DllImport("User32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        /// 
        ///  该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。
        ///  系统给创建前台窗口的线程分配的权限稍高于其他线程。 
        /// 
        /// 将被激活并被调入前台的窗口句柄
        /// 如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零
        [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实现关闭后一直运行

标签:als   .sh   sage   message   end   win   sync   ted   efault   

原文地址:https://www.cnblogs.com/daimaxuejia/p/10795512.html


评论


亲,登录后才可以留言!