WINFORM判断程序是否运行,且只能运行一个实例

来源:互联网 发布:java icmp 编辑:程序博客网 时间:2024/04/29 04:05

WINFORM判断程序是否运行,且只能运行一个实例


判断程序是否已经运行,使程序只能运行一个实例有很多方法,最简单的方法:线程互斥


using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace WFSocket{    static class Program    {        private static System.Threading.Mutex mutex;        /// <summary>        /// アプリケーションのメイン エントリ ポイントです。        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            mutex = new System.Threading.Mutex(true, "OnlyRun");            if (mutex.WaitOne(0, false))            {                Application.Run(new Form1());            }            else            {                MessageBox.Show("プログラムはすでに実行している.", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);                Application.Exit();            }                    }    }}




1 0
原创粉丝点击