0

I have an application that minimizes to icon state when user press close. I want to make application show again when user tries to open the app, instead of just opening the app again.

I thought of using settings: A boolean value, true if the app is opened and false if it isn't but that would mean you should set it to false when user closes app and true in the load function. But since close event doesn't run when program crashes, that would make program un-openable (:D?), and i have no idea how to call events between two programs.

TL;DR: Show the app to user instead of opening it twice when user opens program again, just like Discord.

Thanks!

LIM10
  • 15
  • 1
  • 2
  • 2
    Typically done either using a `Mutex` and or the `Process` way. Does this answer your question [What is the correct way to create a single-instance WPF application?](https://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-wpf-application)? Although tagged with `WPF` it's the same. [Here's another solution](https://stackoverflow.com/questions/34617940/single-instance-of-an-app-in-c-sharp) that may help as well. – Trevor Mar 11 '21 at 14:48

1 Answers1

0

In your Program class add the ShowWindow method from the User32 dll:

const int SW_SHOW = 5;

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

Then write that in your main method before calling the Application.Run method:

var currentProcess = System.Diagnostics.Process.GetCurrentProcess();

// search for another process with the same name
var anotherProcess = System.Diagnostics.Process.GetProcesses().FirstOrDefault(p => p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id);

if (anotherProcess != null)
{
    ShowWindow(anotherProcess.MainWindowHandle, SW_SHOW);
    return; // don't continue to run your application.
}

UPDATE:

The con of that way is that you're not actually identify your program, you're just searching for a process with the same name. so when renaming the exe file you will be able to run an another process of the same program with a different name. or if there is another program in your PC with the same name you will find it as was your program so you will show it instead of yours.

KBar
  • 82
  • 7
  • where do i add the first code (const int etc.)? In the main method or outside – LIM10 Mar 11 '21 at 15:36
  • inside the Program class, outside the main method. – KBar Mar 11 '21 at 15:47
  • This doesn't show the opened program, did i do this right? https://gist.github.com/lim10dev/c144f2cbcfaadc093e9c58665119c06e – LIM10 Mar 11 '21 at 16:10
  • yes. that works for me. is your app minimized and shown in the taskbar? – KBar Mar 11 '21 at 16:29
  • no, its in icon state - background work – LIM10 Mar 11 '21 at 16:31
  • I think it should work anyway since it's the main window of your application but I didn't test it. try it with a form that will be shown in the taskbar to check if that makes the difference. – KBar Mar 11 '21 at 16:45
  • It works fine when the app is minimized – LIM10 Mar 11 '21 at 16:53
  • How to do it for certmgr.msc? It's running as mmc.exe – user3625699 Aug 29 '23 at 11:30
  • @user3625699 Two ideas that I can think of (but didn't try): 1. Inject code to mmc.exe (totally not recommended and can be updated by the Windows Update). 2. Write a program that gets an executable path and checks whether it's already running, if running, it will show its window, otherwise - it will run the executable. You can then create a shortcut to that program with mmc.exe file path as argument and run it instead of running certmgr.msc directly. – KBar Aug 30 '23 at 12:16