I have an option in my application to boot on startup. This used to work fine, but now it doesn't work anymore like it should. When I check the Task Manager right after booting up the system, I can see that the application boots, gets suspended and killed by Windows.
I use the following piece of code to set the Registery key:
private void checkBox_startOnBoot_CheckedChanged(object sender, EventArgs e) //Update settings, set value of registery key
{
if (checkBox_startOnBoot.Checked)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue(Application.ProductName, Application.ExecutablePath);
}
}
else
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue(Application.ProductName, false);
}
}
Settings.Default.Start_On_Boot = checkBox_startOnBoot.Checked;
Settings.Default.Save();
}
I do not see anything in the Main() function that can cause the malfunction:
static void Main()
{
if (Settings.Default.Restarting)
{
Settings.Default.Restarted = true;
Settings.Default.Save();
}
using (oneInstanceMutex = new Mutex(false, "Global\\" + appGuid))
{
if (!oneInstanceMutex.WaitOne(0, false)) // If app already running, but is not restarting // && restartMutex.WaitOne(0, false)
{
if (!Settings.Default.Restarting)
{
AlreadyRunning();
return;
}
}
hpt = new HookProcThread();
hpt.Run();
SystemEvents.PowerModeChanged += OnPowerChange;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mw = new MainWindow();
if (Settings.Default.Restarted)
{
mw.WindowState = (FormWindowState)Settings.Default.RestartingWindowMode;
}
Settings.Default.Restarting = false;
Settings.Default.Save();
Application.Run(mw);
}
}
So if anyone has an idea how I can prevent the application to get suspended and killed on boot, that would be great!
As it turned out, there was a file that couldn't be found. @JeroenMostert and huysentruitw both pointed me in the right direction to look in the event viewer, which showed me the error.