110,535
社区成员
发帖
与我相关
我的任务
分享
Main()
{
if(CheckAppIsRun())
{
SendMessageToActive(); //发送Window消息,重写主窗体的WndProc函数,处理自定义的Window消息
}
else
{
RunApp(); //启动新程序
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Directory_File
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName",out createdNew); //同步基元变量
if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
instance.ReleaseMutex();
}
else
{
MessageBox.Show("已经启动了一个程序!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
}
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Process oProcess = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] aoProcesses = System.Diagnostics.Process.GetProcessesByName(oProcess.ProcessName);
if (aoProcesses.Length > 1)
{
MessageBox.Show(
"程序 \""
+ oProcess.ProcessName
+ "\" 已经运行.", "Ultrasimple");
SetForegroundWindow(aoProcesses[aoProcesses[0].Id ==
oProcess.Id ? 1 : 0].MainWindowHandle);
return;
}
static class Program
{
/// <summary>
/// /// The main entry point for the application.
/// /// </summary>
[STAThread]
static void Main()
{
bool createdNew;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
//每次启动应用程序,都会验证名称为SingletonWinAppMutex的互斥是否存在
Mutex mutex = new Mutex(false, "SingletonWinAppMutex", out createdNew);
//如果已运行,则在前端显示 //createdNew == false,说明程序已运行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
//让程序前段显示并且关闭新启动的程序
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//把Application.Run中的参数改为你的主窗体名
Application.Run(new UpdateVersion());
} /// <summary>
/// /// 查看程序是否已经运行
/// /// </summary>
/// /// <returns></returns>
private static Process GetExistProcess()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if ((process.Id != currentProcess.Id) &&
(Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
{
return process;
}
}
return null;
}
/// <summary>
/// /// 使程序前端显示
/// /// </summary>
/// /// <param name="instance"></param>
private static void SetForegroud(Process instance)
{
IntPtr mainFormHandle = instance.MainWindowHandle;
if (mainFormHandle != IntPtr.Zero)
{
ShowWindowAsync(mainFormHandle, 1);
SetForegroundWindow(mainFormHandle);
}
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
}