日期:2014-05-19  浏览次数:20850 次

如何让程序指运行一个
在Winform程序中我编写一个程序,如何让我的程序只有一个在运行,当此程序运行时不在启动新的程序

------解决方案--------------------

看看用进程来限制只运行一个实例的方法

/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process inst = RunningInstance();

if (inst == null)
{
Application.Run(new EIPNotify());
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);

foreach (Process process in processes)
{
if (process.ProcessName == current.ProcessName && process.Id != current.Id)
{
return process;
}
}
return null;
}

------解决方案--------------------
System.Diagnostics.Process[] mProcs=
System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process sProc in mProcs)
{
if(sProc.ProcessName== "YourProgram.exe ")
{
MessageBox.Show( "程序已经运行 ");
}
}
------解决方案--------------------
经常看到有这样的文章

http://community.csdn.net/Expert/topic/5685/5685446.xml?temp=.7890131
------解决方案--------------------
static void Main()
{
bool createdNew;
System.Threading.Mutex mutex_Application = new System.Threading.Mutex(true, "test ", out createdNew);
if (!createdNew)
{
MessageBox.Show( "本程序只允许同时运行一个! ");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new MNForm());
}
}
------解决方案--------------------
使用Mutex可以搞定,具体参见MSDN,有例子。