Question

I've made an application which needs to run a certain process at Windows startup. I've created a method which does it by receiving the path to the program and adding it to the registry. This is the method:

private void AddPathToStartUpPrograms(string path)
{
  string startUpPosition1 = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  string startUpPosition2 = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  RegistryKey k = Registry.LocalMachine.OpenSubKey(startUpPosition1, true);
  if (k != null)
  {
    k.SetValue("service", path);
  }
  k = Registry.LocalMachine.OpenSubKey(startUpPosition2, true);
  if (k != null)
  {
    k.SetValue("service", path);
  }

}

This code is working but the problem is that it seems that the process I'm adding make the desktop loading get stuck. When the desktop is loading it loads the background wall paper but the icons won't load. I can get to the task manager and when I close the process I've added to the start up programs, the desktop "unfreeze" and finishes loading everything.

What is strange that even though I close the process, after the desktop finish loading, my process runs again and then everything is ok, meaning it for some reason run itself twice.

So my question is how do I set the process to run only AFTER the desktop finished loading?

Was it helpful?

Solution

It runs twice because you're setting the registry key in both Run and RunOnce. If you want it to run each time, just set it in the Run key. The RunOnce key is just for programs that you want to (as the name implies) run only once. The registry entry is automatically removed from RunOnce after the OS starts.

If you want to delay the program until the shell has started up, the easiest way (although a bit hacky) is to just put a Thread.Sleep(60000) at the very start of the the program.

If you want a more sophisticated solution, have a look here: C# - How to know when Windows is "settled" after startup?

OTHER TIPS

What was told about the RunOnce is true and the solution was to simply remove the addition to that registry key. After I've removed it and left the path only in the "Run" key, it was fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top