I've rewritten my answer.Thank you, for Viorel.
(1) Create a class containing Pinvoke declarations.
using System.Runtime.InteropServices;
internal class NativeMethods
{
public const int HWND_BROADCAST = 0xffff;
public static int WM_WAKEUP_WINDOW =
RegisterWindowMessage("{28D5B089-B46F-4F69-99BE-6525596547E6}");
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool PostMessage(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int RegisterWindowMessage(string lpString);
[DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
(2) Program.cs
using System.Diagnostics;
using System.Threading;
internal static class Program
{
[STAThread]
static void Main() {
var current = Process.GetCurrentProcess();
var mutex = new Mutex(true, current.ProcessName, out bool createdNew); ;
if (createdNew) {
try {
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
} finally {
mutex.ReleaseMutex();
}
} else {
NativeMethods.PostMessage(
NativeMethods.HWND_BROADCAST,
NativeMethods.WM_WAKEUP_WINDOW,
IntPtr.Zero, IntPtr.Zero);
}
}
}
(3) Form
protected override void WndProc(ref Message m) {
if (m.Msg == NativeMethods.WM_WAKEUP_WINDOW) {
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
Show();
NativeMethods.SetForegroundWindow(Handle);
return;
}
base.WndProc(ref m);
}