I’m trying to bring up the main window when trying to reopen the app, it works when it’s in a minimized state but it doesn’t work when the window is in a hidden state!
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace Adan
{
public partial class App : System.Windows.Application
{
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern bool IsIconic(IntPtr hWnd);
const int SW_RESTORE = 9;
const int SW_SHOW = 5;
private static Mutex mutex;
protected override void OnStartup(StartupEventArgs e)
{
bool createdNew;
mutex = new Mutex(true, "AdanMutex", out createdNew);
if (!createdNew)
{
IntPtr handle = FindWindow(null, "أذان ليبيا");
if (handle != IntPtr.Zero)
{
// Check if the window is iconic (minimized)
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
else
{
// If window is not visible, show it
if (!IsWindowVisible(handle))
{
ShowWindow(handle, SW_SHOW);
}
}
// Bring the window to the foreground
SetForegroundWindow(handle);
}
else
{
ShowAlreadyRunningMessage();
}
Shutdown();
return;
}
base.OnStartup(e);
}
private void ShowAlreadyRunningMessage()
{
Border border = new Border
{
Width = 300,
Height = 100,
CornerRadius = new CornerRadius(20),
Background = System.Windows.Media.Brushes.White,
Child = new TextBlock
{
Text = "البرنامج يعمل بالفعل",
FontSize = 16,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Center
}
};
Window messageWindow = new Window
{
Width = 300,
Height = 100,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
Content = border
};
messageWindow.Show();
DispatcherTimer timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(20)
};
timer.Tick += (sender, args) =>
{
timer.Stop();
messageWindow.Close();
};
timer.Start();
}
protected override void OnExit(ExitEventArgs e)
{
if (mutex != null)
{
if (mutex.WaitOne(0))
{
mutex.ReleaseMutex();
}
mutex.Dispose();
}
base.OnExit(e);
}
}
}
So had tried everything but not worked to active or bring the hidden app
Does anyone have a solution to my problem or a complete library for Single Instance like what is available in .NET8
New contributor
3sluz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1