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 ?