怎么样判断项目已经启动,避免重复打开程序 up 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce"); bool Running = !mutex.WaitOne(0, false); if (! Running) Application.Run(new frm()); else MessageBox.Show("應用程序已啟動!"); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { //得到正在运行的例程 Process instance = RunningInstance(); if (instance == null) { //如果没有其它例程,就新建一个窗体 Application.Run(new Form1()); } else { //处理发现的例程 //HandleRunningInstance(instance); Application.Exit();//退出 } } //得到正在运行的例程 public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) { //确保例程从EXE文件运行 if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //返回另一个例程实例 return process; } } } //没有其它的例程,返回Null return null; } Process[] p1 = Process.GetProcessesByName("启动的文件名"); [STAThread] static void Main() { bool alreadyExist = false; try { System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess(); System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses(); foreach(System.Diagnostics.Process p in ps) { if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id) { alreadyExist = true; } } } catch(System.PlatformNotSupportedException ex){ex.ToString();} catch(System.InvalidOperationException ex){ex.ToString();} //如果已经存在,则放弃本进程 if(alreadyExist) { MessageBox.Show("程序已经在运行"); return; } Application.Run(new frmMain()); } http://community.csdn.net/Expert/topicview.asp?id=3889111 多种方法实现程序只运行一个:http://xt0055.27h.com/csharp-01-02.htm 方法1: public static void Main(string[] args) { //声明互斥体 System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce"); //判断互斥体是否使用中 bool Running = !mutex.WaitOne(0, false); if (! Running) Application.Run(new FormMain()); else MessageBox.Show("应用程序已经启动!");} 方法2://添加引用using System.Runtime.InteropServices;//申明[StructLayout( LayoutKind.Sequential)]public class SECURITY_ATTRIBUTES { public int nLength; public int lpSecurityDescriptor; public int bInheritHandle; }[System.Runtime.InteropServices.DllImport("kernel32")]private static extern int GetLastError(); [System.Runtime.InteropServices.DllImport("kernel32")]private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName); [System.Runtime.InteropServices.DllImport("kernel32")]private static extern int ReleaseMutex(IntPtr hMutex);const int ERROR_ALREADY_EXISTS = 0183;//调用 static void Main() { IntPtr hMutex; hMutex=CreateMutex(null,false,"test"); if (GetLastError()!=ERROR_ALREADY_EXISTS) { Application.Run(new Form1()); } else { MessageBox.Show("本程序只允许同时运行一个"); ReleaseMutex(hMutex); }} WinForm中,TabControl的Selecting事件中如何得到原来是哪个Tab页 gridview只显示一条记录? 重载listBox.items.add方法 请教各位线程互斥的问题。 分数不多 问个小问题 但是我很着 急急急急急 求datagrid的记录数是用哪个属性啊 .net中有没有类似于java web start的东东 求助:怎样给页面加提示音 请问C#如何打包C的动态库? udp 监听/接收 问题!!!!【在线】 为什么一个只有一个窗体的C# windows应用程序要吃掉10几M的内存? 新手入门,关于new和override的问题,希望高手指点
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new frm());
else
MessageBox.Show("應用程序已啟動!");
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run(new Form1());
}
else
{
//处理发现的例程
//HandleRunningInstance(instance);
Application.Exit();//退出 }
} //得到正在运行的例程
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName); //遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
} //没有其它的例程,返回Null
return null;
}
static void Main()
{
bool alreadyExist = false;
try
{
System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
{
alreadyExist = true;
}
}
}
catch(System.PlatformNotSupportedException ex){ex.ToString();}
catch(System.InvalidOperationException ex){ex.ToString();} //如果已经存在,则放弃本进程
if(alreadyExist)
{
MessageBox.Show("程序已经在运行");
return;
}
Application.Run(new frmMain());
}
http://xt0055.27h.com/csharp-01-02.htm
public static void Main(string[] args)
{
//声明互斥体
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("应用程序已经启动!");
} 方法2://添加引用
using System.Runtime.InteropServices;//申明
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetLastError(); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;//调用
static void Main()
{
IntPtr hMutex;
hMutex=CreateMutex(null,false,"test");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
}