Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:08:38+00:00 2026-06-14T05:08:38+00:00

I know how to make sure that I run only one instance of an

  • 0

I know how to make sure that I run only one instance of an application at a time:
How to check if another instance of the application is running

But… How can I make sure that when a second instance is started, the first instance will be closed (instead of just exiting the 2nd one which is usually done). And preferably waiting for it to be fully closed.

If more than 2 instances are started simultaneously then only the last one should be allowed to ‘live’.

I got it (thanks you all the answers). My solution below:

        #region Kill other running processes of this application
        List<Process> processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).ToList();
        if (processes.Count > 1)
        {
            processes.Sort((x, y) => DateTime.Compare(x.StartTime, y.StartTime));
            for (int i = 0; i < processes.Count - 1; i++)
            {
                processes[i].CloseMainWindow();
                processes[i].WaitForExit();
            }
        }
        #endregion

        Console.WriteLine(string.Format("{0}:{1}", DateTime.Now.Minute, DateTime.Now.Second));
        Console.ReadLine();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:08:39+00:00Added an answer on June 14, 2026 at 5:08 am

    I think you are a little bit confused about the logic yourself. By the way here are some items you should use to do such a thing:

    1- System.Diagnostics.Process.WaitForExit

    With calling this method for a specific process instance, you can wait until the process is exited completely after you called the "Kill" or "Shutdown" method. It will close the main window in the process if it’s a windows app. For the applications which have some confirmation boxes for exiting, it will wait until user confirm or cancel the dialog box (Like when your windows is shutting down while your applications are already open).

    2- System.Diagnostics.Process.ExitCode

    If you want to access the result of the exist message you send to a process, you should call the value of this property after calling "WaitForExit" function. I guess you can use this property to find out if the first process exited or not and with what reason from the second process.

    3- public static int Main

    Because you need to access the exit code from the outside of the process you should change the return value of "Main" method to int. This way you can return a value in the first process and access it in the second process through the "ExitCode" property. If returning a value in the "Main" function is difficult in your situation, you can pass the exit code by calling the 4th item.

    4- System.Windows.Application.Shutdown (Exit in .NET < 4)

    Calling this method does the exact thing as returning the value in the "Main" function.

    5- System.Diagnostics.Process.GetProcessesByName

    After all by calling this static method you can get all the processes by specified name.

    Hope it helps. I just tried to explain everything as simple as possible. These all are the tools you can use to implement the logic. The rest would be so simple if you know these abilities that .NET provided for you.

    After all, as I’ve already forgot to say, if you are interested in doing the right thing, in my opinion and as my experience says, there is a technology that has been made just for these kind of logics.

    6- .NET Interprocess Communications or IPC (a sample on code) (introduction on codeguru)

    IPC is based on the .NET remoting (the most interesting discussion in .NET in my personal opinion) BUT regardless of what the name says, it’s not only for remote communications. The remoting has many sections and on of those is the Inter-Process-Communication (IPC). It’s the hard, but the best way if you ask me.

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that I want to make sure only one is running,
Do you know if it is possible to make sure that a JavaScript script
Guys I know this question is silly but just to make sure: Having in
I don't know how make my application as auto start application.That is my expectation
I have an event handler, but I want to make sure that it will
I have to make sure that my Service will run until I'll explicitly call
I want to know how to announce int to make sure it's 4 bytes
I'm thinking about frameworks. I know they make life a lot easier, but as
I know how to make the list of the Fibonacci numbers, but i don't
I have a fairly simple iPhone application that I want to have run on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.