using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;前端
namespace AppLed
{
static class Program
{
//防止程序運行多個實例的方法有多種,如:經過使用互斥量和進程名等.而我想要實現的是:在程序運行多個實例時激活的是第一個實例,使其得到焦點,並在前端顯示.
//主要用到兩個API 函數:
//ShowWindowAsync 該函數設置由不一樣線程產生的窗口的顯示狀態。
//SetForegroundWindow 該函數將建立指定窗口的線程設置到前臺,而且激活該窗口。鍵盤輸入轉向該窗口,併爲用戶改各類可視的記號。系統給建立前臺窗口的線程分配的權限稍高於其餘線程。api
/// <summary>
/// 該函數設置由不一樣線程產生的窗口的顯示狀態。
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="cmdShow">指定窗口如何顯示。查看容許值列表,請查閱ShowWlndow函數的說明部分。</param>
/// <returns>若是函數原來可見,返回值爲非零;若是函數原來被隱藏,返回值爲零。</returns>
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
/// <summary>
/// 該函數將建立指定窗口的線程設置到前臺,而且激活該窗口。鍵盤輸入轉向該窗口,併爲用戶改各類可視的記號。系統給建立前臺窗口的線程分配的權限稍高於其餘線程。
/// </summary>
/// <param name="hWnd">將被激活並被調入前臺的窗口句柄。</param>
/// <returns>若是窗口設入了前臺,返回值爲非零;若是窗口未被設入前臺,返回值爲零。</returns>
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;函數
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new FrmLed());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 獲取正在運行的實例,沒有運行的實例返回null;
/// </summary>
public static Process RunningInstance()
{
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;
}post
/// <summary>
/// 顯示已運行的程序。
/// </summary>
public static void HandleRunningInstance(Process instance)
{
//MessageBox.Show("ID:"+instance.Id .ToString()+"--句柄"+instance.MainWindowHandle.ToString() + "--正常窗口" + WS_SHOWNORMAL + "--" + ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL) + "--" + SetForegroundWindow(instance.MainWindowHandle));
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //顯示,能夠註釋掉
SetForegroundWindow(instance.MainWindowHandle); //放到前端
}
}
}spa
2種方法 線程
#region 確保程序只運行一個實例 private static Process RunningInstance() { 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; } private static void HandleRunningInstance(Process instance) { MessageBox.Show("監控系統已經在運行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowWindowAsync(instance.MainWindowHandle, 1); //調用api函數,正常顯示窗口 SetForegroundWindow(instance.MainWindowHandle); //將窗口放置最前端 } [DllImport("User32.dll")] private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(System.IntPtr hWnd); #endregion
只有窗口最小化的時候能夠達到此效果,若是隱藏到托盤則沒法將打開的程序顯示到桌面orm