【源码】Fast2011 启动主进程只有一个

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;//引入Process 类
using System.Reflection;//引入Assembly
using System.Runtime.InteropServices; //需要获取句柄,激活前一实例
using System.Threading;//需要用到mutex

namespace fasta2011
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
}
public static Process GetRunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);

foreach (Process process in processes)
{
if (process.Id != current.Id)
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
return process;
}
return null;
}

public static void HandleRunningInstance(Process instance)
{

ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(instance.MainWindowHandle);
}
}
}

posted @ 2011-10-25 17:48  心_远  阅读(175)  评论(0编辑  收藏  举报