控制窗口打开一次 private void mybutton_add_Click(object sender, EventArgs e)
{
FormCollection form_is_running = Application.OpenForms;
if (form_is_running["Form_add"] != null)
{
(form_is_running["Form_add"] as Form).Focus();
}
else
{
Form_add form_add = new Form_add();
form_add.Show();
}
}
控制程序只运行一次
Class Form1:Form
{
public Mutex mymutex;
//-------------------------------在构造函数中加入
public Form1()
{
mymutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
if (!mymutex.WaitOne(0, false))
{
mymutex.Close();
mymutex = null;
}
}
}
//_-------------------------------Dispose方法中加入一行
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
mymutex.ReleaseMutex(); //---------此行
}
base.Dispose(disposing);
}
//----------------------------------Main方法中,如果是不同类,请建立实例。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
if (form1.mymutex != null)
Application.Run(form1);
}
}