winform程序防止重复运行
2011-08-19 11:15
357 查看
用互斥法实现防止程序重复运行,使用内核对象Mutex可以防止同一个进程运行两次。注意:是名称相同的进程,而不是exe,因为exe程序可以改名。
在Program.cs中修改
首先添加using System.Threading;引用
然后原内容改为下面所示
static class Program
{
/// <summary>
/// 互斥法防止程序重复运行
private static Mutex myMutex;
private static bool requestInitialOwnership = true;
private static bool mutexWasCreated;
/// </summary>
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//原来代码
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//新代码
myMutex = new Mutex(requestInitialOwnership, "myExe ", out mutexWasCreated);
if (mutexWasCreated)
{
Application.Run (new Form1());
myMutex.WaitOne();
}
}
}
在Program.cs中修改
首先添加using System.Threading;引用
然后原内容改为下面所示
static class Program
{
/// <summary>
/// 互斥法防止程序重复运行
private static Mutex myMutex;
private static bool requestInitialOwnership = true;
private static bool mutexWasCreated;
/// </summary>
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//原来代码
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//新代码
myMutex = new Mutex(requestInitialOwnership, "myExe ", out mutexWasCreated);
if (mutexWasCreated)
{
Application.Run (new Form1());
myMutex.WaitOne();
}
}
}
相关文章推荐
- Winform防止程序重复运行
- winform程序防止重复运行
- C# .net防止一个程序(WinForm)重复运行的方法。
- [WinForm]C# .net防止一个程序(WinForm)重复运行的方法。
- [WinForm]C# .net防止一个程序(WinForm)重复运行的方法。
- WinForm防止程序重复运行的方法分析
- C# .net防止一个程序(WinForm)重复运行的方法。
- vc++ 防止程序重复运行
- c++builder防止程序重复运行 转贴
- vc防止运行多个程序,重复运行,同一个程序
- [转]防止程序重复运行,显示原来的窗口
- 防止程序重复运行
- Vb.Net防止同一程序重复运行的方法
- vc 防止程序重复运行(即只运行一个实例)并激活已运行进程和枚举进程及窗口
- C#防止程序重复运行
- vc 禁止程序同时运行多份(防止重复运行)
- #pragma data_seg 实现防止程序重复运行
- 忆龙2009:windows mobile防止程序重复运行
- windows内核编程之如何防止一个相同的程序重复运行
- 忆龙2009:windows mobile防止程序重复运行