您的位置:首页 > 编程语言 > C#

C# Winform 只能运行一个实例笔记

2009-05-18 15:43 441 查看
这里列举了比较常见的方法:

1、

static void Main()

{

bool initiallyOwned = true;

bool isCreated;

Mutex m = new Mutex(initiallyOwned,"MyTest",out isCreated);

if (!(initiallyOwned && isCreated))

{

MessageBox.Show("已经有相同的实例在运行。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

Application.Exit();

}

else

{

Application.Run(new MainForm());

}

}

2、

static void Main()

{

int ProceedingCount = 0;

Process[] ProceddingCon = Process.GetProcesses();

foreach(Process IsProcedding in ProceddingCon)

{

if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)

{

ProceedingCount += 1;

}

}

if(ProceedingCount > 1)

{

MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

}

else

{

Application.EnableVisualStyles();

Application.DoEvents();

Application.Run(new MainForm());

}

}

3、

private static bool CheckInstance()

{

string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;

if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)

{

return true;

}

else

{

return false;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: