C#实现只许一个实例运行
2012-04-10 08:45
351 查看
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace TST.SINGLE { publicclass TSTPGM { ///<summary> /// The main entry point for the application. ///</summary> [STAThread] staticvoid 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) { IntPtr hWnd = process.MainWindowHandle; ShowWindowAsync(hWnd, WS_SHOWNORMAL); //调用api函数,正常显示窗口 SetForegroundWindow(hWnd);//将窗口放置最前端。 break; } } } } } [DllImport("User32.dll")] private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; } }
相关文章推荐
- C#实现只许一个实例运行
- C#实现只许一个实例运行(使用mutex类)
- C#实现只许一个实例运行(使用mutex类)
- 使用C#实现只允许运行一个程序实例的几种方法
- 转载:C#只允许运行应用程序的一个实例的正确实现方法
- C#利用Mutex实现只运行一个程序实例
- c#实现一个程序在一台电脑上只运行一个实例
- C#利用Mutex实现只运行一个程序实例
- c# Winform 如何实现程序只能同时运行一个实例
- C#利用Mutex互斥量实现同时只有一个进程实例在运行
- C#程序只允许运行一个实例的解决方案
- 运行一个实例(进程)功能的多种常用实现
- C#程序只允许运行一个实例的解决方案
- c#学习笔记 只运行一个实例
- C#winform中只允许程序运行一个实例
- C#开发Windows服务实例之实现禁止QQ运行
- [C#]实现IEnumerable接口来使用foreach语句的一个实例
- C#只运行一个实例
- 让程序运行一个实例在Ireaper中实现
- 使用Mutex实现程序单实例运行(c#)