using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace RunOnceByMutex { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); }
private void Frm_Main_Load(object sender, EventArgs e) { bool Exist;//定义一个bool变量,用来表示是否已经运行 //创建Mutex互斥对象 System.Threading.Mutex newMutex = new System.Threading.Mutex(true,"仅一次",out Exist); if (Exist)//如果没有运行 { newMutex.ReleaseMutex();//运行新窗体 } else { MessageBox.Show("本程序一次只能运行一个实例!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//弹出提示信息 this.Close();//关闭当前窗体 } } } }