0

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?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 1
    Why do you add it to both startup keys? Wouldn't that cause it to start twice the first time? Also, why add it to RunOnce in the first place? That should only be used for things that should only run once (entries are deleted from that key after a startup). Also, since you name your value "service", why don't you register a Windows Service? That could be configured to start delayed. – cremor Jul 29 '13 at 07:42
  • I was adding it to both places since when I tried searching on how to make a program run at startup, every place named these 2 places to insert the paths to... So what actually should be done? – CodeMonkey Jul 29 '13 at 07:47
  • If you really want to start an application that the user can use (e.g. a tray icon), use the solution that was given by Matthew Watson. If your program should run in the background without any user interaction, you should create a real Windows Service. – cremor Jul 29 '13 at 07:51

2 Answers2

1

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?

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • for those who read it, the part about the runOnce is true, but the problem was solved by simply not writing to that registry location... not by adding a thread sleep – CodeMonkey Jul 29 '13 at 11:29
0

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.

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203