하나의 프로세스만 실행할 수 있는 실례
2879 단어 인스턴스
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
[STAThread]
public static void Main()
{
//
Process instance = RunningInstance();
if (instance == null)
{
// ,
Application.Run(new Form());
}
else
{
//
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//
foreach (Process process in processes)
{
//
if (process.Id != current.Id)
{
// EXE
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//
return process;
}
}
}
// , Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
// foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[ Java ] 7장. 객체, 인스턴스화 ( + 자바 특징 )객체화(인스턴스화)는 코드를 실제 세계의 사물들로 표현하는 것 객체마다 고유한 특성과 행동을 지님 다른 객체와 정보를 주고 받고 상호작용을 함 컴퓨터에서 객체는 테트리스의 블록이나 한글 프로그램의 메뉴나 버튼들을 의...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.