Домой Windows Form Один экземпляр приложения: Справочник по C#

Один экземпляр приложения: Справочник по C#

544
0


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;

namespace SingleInstanceTest
{
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

public static bool HasPriorInstance()
{
Process currentProcess = Process.GetCurrentProcess();
string fileName = currentProcess.StartInfo.FileName;
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id == currentProcess.Id) { continue; }
if (process.StartInfo.FileName != fileName) { continue; }
SetForegroundWindow(process.MainWindowHandle);
return true;
}
return false;
}

[STAThread]
static void Main()
{
if (!HasPriorInstance())
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}

Взято с http://www.dreamincode.net

Один экземпляр приложения: Справочник по C#

0.00 (0%) 0 votes

ЧИТАТЬ ТАКЖЕ:  Проверка доступности сетевого адреса: Справочник по C#

ОСТАВЬТЕ ОТВЕТ

Пожалуйста, введите ваш комментарий!
пожалуйста, введите ваше имя здесь