c# 程序只能运行一个实例
2011-11-22 22:09
429 查看
程序只能运行一个程序
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace SystemTray
{
static class Program
{
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
//如果沒有其他常式,就新建一個表單
//Application.Run(new Form());
Application.Run(new ClockWaring());
}
else
{
//處理發現的常式
HandleRunningInstance(instance);
}
}
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;
}
public static void HandleRunningInstance(Process instance)
{
//確保視窗沒有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//設置真實常式為foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[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;
}
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace SystemTray
{
static class Program
{
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
//如果沒有其他常式,就新建一個表單
//Application.Run(new Form());
Application.Run(new ClockWaring());
}
else
{
//處理發現的常式
HandleRunningInstance(instance);
}
}
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;
}
public static void HandleRunningInstance(Process instance)
{
//確保視窗沒有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//設置真實常式為foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[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;
}
}
相关文章推荐
- C#winform判断程序已经运,且只能运行一个实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- 【转】C#只能运行一个实例程序的方法
- C#只能运行一个实例程序的方法
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例(转)
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# Winform 如何实现程序只能同时运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- [C#]只能运行程序的一个实例
- C# 让程序只能运行一个实例
- C# 创建互斥进程(程序只能运行一个实例)
- C# WINFORM判断程序是否运行,且只能运行一个实例