C#让程序只运行一次实例
2007-02-28 14:15
337 查看
添加引用
调用DLL文件
添加如下两个函数
在MAIN中添加如下代码
using System.Reflection; using System.Runtime.InteropServices; using System.Diagnostics;
调用DLL文件
[DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1;
添加如下两个函数
public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } public static void HandleRunningInstance(Process instance) { //Make sure the window is not minimized or maximized ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //Set the real intance to foreground window SetForegroundWindow(instance.MainWindowHandle); }
在MAIN中添加如下代码
public static void Main() { Process instance = RunningInstance(); if (instance == null) { Application.Run(new Form1()); } else { HandleRunningInstance(instance); } }
相关文章推荐
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- C#让程序只运行一次实例
- C# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)
- wince c# 程序只能运行一次
- 保证C#程序只运行一次
- .NET限制程序一次只能运行一个实例
- 使用Mutex实现程序单实例运行(c#)
- C# WinForm判断程序是否运行,且只能运行单个实例