c# 应用程序只运行一个实例
2011-04-21 15:38
218 查看
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace OneInstance
{
/// <summary>
/// Class with program entry point.
/// </summary>
internal sealed class Program
{
//Api声明,并声明一
[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;
[STAThread] public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new MainForm());
}
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);
}
}
}
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
namespace OneInstance
{
/// <summary>
/// Class with program entry point.
/// </summary>
internal sealed class Program
{
//Api声明,并声明一
[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;
[STAThread] public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new MainForm());
}
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);
}
}
}
相关文章推荐
- C#只允许运行应用程序的一个实例的正确写法
- 用C#让应用程序只有一个实例在运行
- C#如何让应用程序只有一个实例在运行
- C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 保证应用程序只运行一个实例[c#]
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- 转载:C#只允许运行应用程序的一个实例的正确实现方法
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 应用程序每次只能运行一个实例(C#)
- C#在终端服务器只允许运行应用程序的一个实例
- 用C#让应用程序只有一个实例在运行
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 应用程序每次只能运行一个实例(C#)转
- 如何让应用程序仅运行一个实例(c#)
- C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper
- C#应用程序只运行一个实例的代码
- 如何让应用程序仅运行一个实例(c#)