C#实现只许一个实例运行(使用mutex类)
2010-09-30 17:46
633 查看
在google上搜了很多(全是中文的),找了半天也没有解决问题。
最后没办法,只能搜索e版的了。于是找到了下面的答案。
metex是mutual exclusion 的缩写,
完整的实现参照一下:
代码
最后没办法,只能搜索e版的了。于是找到了下面的答案。
metex是mutual exclusion 的缩写,
完整的实现参照一下:
代码
using System.Runtime.InteropServices; namespace TST.SINGLE { public class TSTPGM { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { bool createdNew = true; using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew)) { if (createdNew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } else { Process current = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(current.ProcessName)) { if (process.Id != current.Id) { SetForegroundWindow(process.MainWindowHandle); break; } } } } } } }
相关文章推荐
- C#实现只许一个实例运行(使用mutex类)
- 使用C#实现只允许运行一个程序实例的几种方法
- C#实现只许一个实例运行
- C#实现只许一个实例运行
- 使用Mutex实现程序单实例运行(c#)
- c#实现一个程序在一台电脑上只运行一个实例
- 使用Mutex实现程序单实例运行(c#)
- 使用互斥体Mutex 实现应用程序只有一个实例运行
- 使用进程互斥实现WinForm程序只运行一个实例(VB.Net)
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- 使用进程互斥实现WinForm程序只运行一个实例(VB.Net)
- c# Winform 如何实现程序只能同时运行一个实例
- C#使用Mutex简单实现程序单实例运行的方法
- 使用Mutex实现程序单实例运行(c#)
- [C#]实现IEnumerable接口来使用foreach语句的一个实例
- C#利用Mutex互斥量实现同时只有一个进程实例在运行
- C#使用Mutex实现程序单实例运行
- 转载:C#只允许运行应用程序的一个实例的正确实现方法
- 使用Mutex实现程序单实例运行(c#)
- C#使用Mutex实现系统范围内单实例运行的示例代码