-2

Inside Program.cs i did:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;

namespace mws
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                if (IsApplicationAlreadyRunning() == true)
                {
                    MessageBox.Show("The application is already running");
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }
            catch (Exception err)
            {
                Logger.Write("error " + err.ToString());
            }
        }
        static bool IsApplicationAlreadyRunning()
        {
            string proc = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(proc);
            if (processes.Length > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

I used a breakpoint and even if i closed my program in this from visual studio and i try to run the application again it return true and throw the message that the application is already runnig.

In the breakpoint i saw that the variable proc contain: My Weather Station.vshost

And now i noticed this happen only if i open another copy of my application in a new visual studio window and the application is on another location on another hard disk and i didn't run the application not on any of the visual studios windows.

So why when i have the same application opened twice in visual studio and it's not running it's thinking that it is running ? And even if i close the second visual studio i still see in proc My Weather Station.vshost but this time it return false.

EDIT

I see now that the variable processes contain twice in index 0 and 1 My Weather Station.vshost And if i close the second visual studio it contain only one.

The question is what is this My Weather Station.vshost ? And why it count it as running application if the application is not running ? How can i make this checking better so if i open more copies of my application it will not think the app is running ?

The reason i wanted to open another copy of my application is that the second one is older and i wanted to see some old things i did.

I can change this:

if (processes.Length > 1)

And make it > 10 for example But still i don't understand what is My Weather Station.vshost ? Why it's running in the memory ? And why it's considering it as running application ?

Daniel Bout
  • 133
  • 2
  • 3
  • 10
  • If you are trying to make sure only 1 instance is running try this thread `http://stackoverflow.com/questions/184084/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-windows` – KSdev Oct 08 '14 at 19:33
  • possible duplicate of [Prevent application launch if already running](http://stackoverflow.com/questions/18477502/prevent-application-launch-if-already-running) – Dour High Arch Oct 08 '14 at 19:35
  • possible duplicate of [Return to an already open application when a user tries to open a new instance](http://stackoverflow.com/questions/94274/return-to-an-already-open-application-when-a-user-tries-to-open-a-new-instance) – Rowland Shaw Oct 08 '14 at 19:44
  • Here's what I use - works for me: http://stackoverflow.com/questions/25538524/how-can-i-get-rid-of-an-abandoned-mutex/25538839#25538839 – RenniePet Oct 08 '14 at 19:45

1 Answers1

1

It seems you are trying to create a "single instance application". There are several existing solutions published for this. The generally accepted way to do this is to make use of a mutex and see if it is already held, like here: What is the correct way to create a single-instance application?

Additionally, it appears this is a feature already built into windows forms (which based on the references in your example source file, you seem to use). Refer to Prevent application launch if already running

Community
  • 1
  • 1
Alex
  • 13,024
  • 33
  • 62