C# 单实例运行控制

来源:互联网 发布:linux mint设置中文 编辑:程序博客网 时间:2024/05/22 16:42
static class Program    {                [DllImport("User32.dll")]        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);        [DllImport("User32.dll")]        private static extern bool SetForegroundWindow(IntPtr hWnd);        [DllImport("User32.dll")]        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        private const int WS_SHOWNORMAL = 1;                /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {                       bool MutexWasCreated;            Mutex MyMutex = new Mutex(true, "OCRServer", out MutexWasCreated);            if (MutexWasCreated)            {                Application.EnableVisualStyles();                Application.SetCompatibleTextRenderingDefault(false);                Application.Run(new FrmMain());                MyMutex.WaitOne();            }            else            {                                //“智能识别”是窗体的标题名称IntPtr ptr = FindWindow(null, "智能识别");                if (ptr != IntPtr.Zero)                {                    ShowWindowAsync(ptr, WS_SHOWNORMAL);                    SetForegroundWindow(ptr);                }                            }                    }    }

0 0
原创粉丝点击