大连融科 WMS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace SiaSun.LMS.Common
{
/// <summary>
/// Ӧ�ó���Ψһ����ʵ��ʵ��
/// ����ǰϵͳֻ�ܴ���һ��SiasunLMS����ʵ��
/// </summary>
public static class SingleInstance
{
/// <summary>
/// �����������еij���ʵ���߳�
/// </summary>
public static Process GetRunningInstance()
{
Process currentProcess = Process.GetCurrentProcess(); //��ȡ��ǰ����
//��ȡ��ǰ���г�����ȫ�޶���
string currentFileName = currentProcess.MainModule.FileName;
//��ȡ������ΪProcessName��Process���顣
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//��������ͬ���������������еĽ���
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id) //���ݽ���ID�ų���ǰ����
return process;//���������еĽ���ʵ��
}
}
return null;
}
//��������������WinAPI���书�ܽ��ڰ�װ������������
[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;
/// <summary>
/// ������һ����װ��HandleRunningInstance��̬����Ϊ��ȡӦ�ó�������������Ӧ�ó���Ϊǰ̨���У�������boolֵ��
/// </summary>
/// <param name="instance">����ʵ��</param>
public static bool HandleRunningInstance(Process instance)
{
//ȷ������û�б���С����������
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//����Ϊforeground window
return SetForegroundWindow(instance.MainWindowHandle);
}
/// <summary>
/// �ж��Ƿ�ʵ���Ƿ�����
/// </summary>
public static bool HandleRunningInstance()
{
Process p = GetRunningInstance();
if (p != null)
{
HandleRunningInstance(p);
return true;
}
return false;
}
}
}