C#
using System.Diagnostics;
......
......
Process[] a=System.Diagnostics.Process.GetProcessesByName("你的程序名字");
if(a.Length>1)
{
Console.WriteLine("程序已运行");
return;
}
using System.Diagnostics;
......
......
Process[] a=System.Diagnostics.Process.GetProcessesByName("你的程序名字");
if(a.Length>1)
{
Console.WriteLine("程序已运行");
return;
}
以前在applet里面可以, 甚至可以用来传递数据 :)
{
//保证同时只有一个客户端在运行
System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false, "MyApplication");
if(!mutexMyapplication .WaitOne(100, false))
return;
我用的好好的。我就是用上面的方法的呀!
另外楼主搜索一下以前的贴子。
bool pblnGotOwnership = false;
Mutex pobjMyMutex = null;
try
{
pobjMyMutex = new Mutex(true, "只运行一个进程", out pblnGotOwnership); if (pblnGotOwnership)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("只能运行一个程序员");
}
if(pobjMyMutex != null)
{
pobjMyMutex.Close();
}
}
catch(Exception exce)
{
MessageBox.Show(exce.Message.ToString());
}
}
⒈判断进程法:
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if ( process.MainModule.FileName
== current.MainModule.FileName)
{
MessageBox.Show("程序已经运行!",Application.ProductName,
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return ;
}
}
}
⒉创建互斥体法:
bool blnIsRunning;
Mutex mutexApp = new Mutex(false,Assembly.GetExecutingAssembly().FullName,out blnIsRunning);
if(!blnIsRunning)
{
MessageBox.Show("程序已经运行!",Application.ProductName,
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return ;
}
以上代码仅供参考!
如需更详细的示例,可将您的E-mail用短信的方式发给我,本人将及时发放!