Winform and WPF 第二遍双击快捷方式或应用程序打开原来的应用程序而不新建一个实例[进程通信 1]
private
void
Window_Loaded(
object
sender, RoutedEventArgs e)
{
Process[] pro = Process.GetProcesses();
int
n = pro.Where(p => p.ProcessName.Equals(
"进程名称"
)).Count();
if
(n > 1)
{
Application.Current.Shutdown();
return
;
}
}
今天工作忙完之余,总结一些Winform WPF应用程序,第二遍双击快捷方式或应用程序打开原来的应用程序而不新建一个实例。 下面两个例子分别用了两种不同的方式实现了进程间的通信。
---------------------------------------------------------------------------WinformStart---------------------------------------------------------------------
File 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using XXXSDK;
using System.IO;
namespace Server
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new Form1());
}
else
{
HandleRunningInstance();
}
}
private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id == current.Id)
{
continue;
}
// Get first start-up application.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return process;
}
}
return null;
}
private static void HandleRunningInstance()
{
IntPtr hWnd = NativeMethodsExtensibility.FindWindow(null, "Form1");
if (hWnd != IntPtr.Zero)
{
NativeMethodsExtensibility.ShowWindowAsync(hWnd, NativeMethodsExtensibility.SW_SHOWNORMAL);
NativeMethodsExtensibility.SetForegroundWindow(hWnd);
}
else
{
}
}
}
}
File 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace XXXSDK
{
/// <summary>
/// DllImport Win32API
/// </summary>
public static partial class NativeMethodsExtensibility
{
#region DllImport
[DllImport("User32.dll", EntryPoint = "SendMessage")]
internal static extern int SendMessage
(
IntPtr hWnd, // Get target handle
int Msg, // WM_COPYDATA
int wParam, // Customized data
ref COPYDATASTRUCT lParam // Struct
);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
internal static extern int SendMessage(
int hWnd, // Get target handle
int Msg, // WM_COPYDATA
int wParam, // Customized data
ref COPYDATASTRUCT lParam // Struct
);
[DllImport("user32.dll")]
internal static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "ShowWindowAsync")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
internal static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
internal static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32.dll")]
internal static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll")]
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
[DllImport("user32")]
internal static extern int EnumWindows(CallBack x, int y);
[DllImport("user32.dll")]
internal static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
internal static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
//ShowWindow参数
public const int SW_SHOWNORMAL = 1;
public const int SW_MAXIMIZE = 3;
public const int SW_RESTORE = 9;
public const int SW_SHOWNOACTIVATE = 4;
//SendMessage参数
public const int WM_KEYDOWN = 0X100;
public const int WM_KEYUP = 0X101;
public const int WM_SYSCHAR = 0X106;
public const int WM_SYSKEYUP = 0X105;
public const int WM_SYSKEYDOWN = 0X104;
public const int WM_CHAR = 0X102;
internal delegate bool CallBack(int hwnd, int lParam);
#endregion
#region Native Values
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public const int WM_COPYDATA = 0x004A;
#endregion
}
}
---------------------------------------------------------------------------WinformEnd---------------------------------------------------------------------
---------------------------------------------------------------------------WPFStart-------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using PTMC.SDK;
namespace Microsoft.Protocols.TestSuiteStudio
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : System.Windows.Application
{
public App()
{
bool createNew;
string targetExeName = System.Reflection.Assembly.GetExecutingAssembly().Location;
string productName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);
using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, productName, out createNew))
{
if (createNew)
{
//MainWindow.xaml
StartupUri = new System.Uri("MainWindow.xaml", UriKind.Relative);
Run();
}
else
{
PTMCWin32API.SendMessage(targetExeName, "Protocol Testing Management Console", "/v:true");
System.Environment.Exit(1);
}
}
}
}
}
---------------------------------------------------------------------------WPFEnd---------------------------------------------------------------------------