Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start runs twice

Tags:

c#

windows

I'm facing a weird problem. I have an executable (Exe1) which I register in the RunOnce key (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce) Exe1 is run on the next restart. In Exe1, I'm using Process.Start() to launch another executable (Exe2). The syntax is

string Installer = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Exe2.exe";
Process.Start(Installer);

Strangely, the Exe2 is launched twice sometimes and sometimes not. I'm not able to figure out how. Exe1 is launched only once at the startup. I'm sure - I'm taking these steps.

  • I'm using a mutex and exiting Exe1 if the mutex exists before calling Process.Start().
  • I'm writing a log before calling Process.Start() - that's also recorded only once
  • I'm also checking if Exe2 is already running before Process.Start() by walking through Process.GetProcesses()

In spite of all this, Exe2 is launched twice. And this happens when its started from RunOnce on reboot. It does not happen if I launch Exe1 directly.

Appreciate any help or useful information

like image 722
Balok Avatar asked Jun 09 '26 13:06

Balok


1 Answers

I'll speak to the issue I encountered since it may be close enough to your issue. First, I placed a break point everywhere I expected my process to start. If the process starts without me continuing, then my code triggered the process elsewhere.

Process myProcess = Process.Start(startInfo);
myProcess.Start();

I placed a break point on the second line, but clearly I was triggering the process on the first line. Those wonderfully (annoying) simple mistakes are sometimes hard to spot if the code is dispersed. Hope that helps or anyone else hitting this issue.

like image 72
Wario X Avatar answered Jun 12 '26 12:06

Wario X



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!