I made a simple winforms
application and it contains a single form. The problem is when I click the executable I want the activated form to get focused instead of multiple instances of the application. I tried to solve this problem this way. This only prevents multiple instances, but can't get focus on the activated form. This code is from Program.cs
file.
namespace Shutdown_Scheduler
{
static class Program
{
[STAThread]
static void Main()
{
string processName = Process.GetCurrentProcess().ProcessName;
Process[] processCollection = Process.GetProcessesByName(processName);
if (processCollection.Length > 1)
{
Process.GetCurrentProcess().Kill();
ProgramEntry();
}
else
{
ProgramEntry();
}
}
static void ProgramEntry()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}