Tell me how to distinguish between the execution of commands, to first perform the removal of the key in the registry, then start the external application, after which the parameters should return to the registry, and the application will exit.

This is necessary so that the command to delete the registry key has time to perform actions before opening the external application, and the application has time to start before the registry settings return to their original state.

How it works - the file is transferred to the application, the application reads the file arguments, then by code:

if (args.Any()) { key = Registry.LocalMachine.OpenSubKey("MyAapp\\BranchReg", true); key.DeleteValue("KeyReg", false); key.Close(); Process.Start(args[0]); key = Registry.LocalMachine.CreateSubKey("MyAapp\\BranchReg"); key.SetValue("KeyReg", ""); key.Close(); Application.Exit(); } 
  • If you need synchronization of processes, you can try to use some type of global lock, for example, a mutex . - tym32167
  • 2
    If the application is with a GUI, you can use WaitForInputIdle . Also see stackoverflow.com/q/6390030/5045688 - Alexander Petrov

1 answer 1

  Process process = new Process(); process.Start(); process.WaitForExit(); 
  • Unfortunately, WaitForExit will cause the application to wait for the completion of the external application process, and only then return the registry settings to their original state ... - Vitokhv
  • 2
    @Vitokhv, as I understand it, it’s necessary that the value change to "" only after the application we are running does something, but it can continue its work i.e. his expectation does not suit us. while the variant may try to get the value in the loop after the launch from the required branch and after the value is successfully received, change it to "". well, or look towards RegNotifyChangeKeyValue - NMD
  • All of which I fear is that the code may accidentally outperform the launch of an external application, for example, an external application has not yet managed to start, and the registry parameter has already returned to its original state. - Vitokhv
  • What about Thread.Sleep(10); I think 10 milliseconds will be enough to separate the teams at least in time. But complain about this method (such as there may be errors). - Vitokhv
  • at the expense of Sleep, this is the type of reserve for future "optimization" - NMD