如何让程序只运行一次实例
来源:互联网 发布:崩坏3矩阵探索 回血 编辑:程序博客网 时间:2024/04/28 16:26
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
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);
}
[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;
}
- 如何让程序只运行一次实例
- 如何让程序只运行一次实例
- 【转贴】如何让程序只运行一次
- 如何让程序只运行一次(BCB)
- WINCE-如何只让程序运行一次
- 如何让程序只运行一个实例
- 让程序只运行一次
- 让程序只运行一次
- 让程序只运行一次
- 如何让Qt程序只运行一次等
- 让程序只运行一次(Delphi)
- C#让windows程序只运行一次
- C#让windows程序只运行一次
- Delphi 让程序只运行一次
- c#让windows程序只运行一次
- 让单文档程序只运行一次
- C#让windows程序只运行一次
- win32 让程序只运行一次
- java操作word,pdf的四种武器
- servlet实现从oracle数据库的blob字段中读出文件并显示。
- 测试:我的第一篇文章
- 法国印象
- My Niece Is Born
- 如何让程序只运行一次实例
- LOG4J的配置
- 知识管理的类型与开发
- 在天堂与地狱之间——清华浪子梦断中关村[推荐]
- 未能收到OCP证书的处理流程
- 友情提醒:年底春节就快来了,大家不要轻意辞职,凡事都过完年再说。[欢迎大家转贴]
- 设计模式-简单工厂模式(SimpleFactory-C#)
- Sun开放Java源代码 借助外力参与程序设计
- 持续集成 Java手册