Start application just one time C# [duplicate]
-
21-06-2021 - |
Вопрос
I just make an application in C#, but something I don't like. I want to make the application running just one time. Now, if my application is running, when I run the application again, will start another application. I want to make that the application can be running just one time, if you want to run again the application, not create another application, but to start the running application.
Thanks.
Решение
Please check this question: Run single instance of an application using Mutex
You are looking for Mutex
to check if you application is running. Then find your running application Process
and bring it forward.
Другие советы
The way I have achieved this is to use a Mutex
. Each instance of the application tries to lock the mutex at startup. If they succeed, they get to run; if they fail, they don't.
The application that doesn't get to run also restores and activates the running instance. This is achieved differently depending on whether it's a WPF or WinForms application.
The advantage to the mutex approach is that it is less prone to race conditions than using FindWindow.
when your application start you have to see the processes that your instance of application is already running or not then make decision according to it.
Possible duplicates :
Detecting a Process is already running in windows using C# .net
The concept you are referring is "single instance application". This is usually achieved by named waitable objects, have a look at this article, or this one.