码迷,mamicode.com
首页 > Windows程序 > 详细

winfom实现关闭后一直运行

时间:2019-04-30 14:12:48      阅读:152      评论:0      收藏:0      [点我收藏+]

标签: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;
        /// <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实现关闭后一直运行

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!