0

I have a parent program that calls a worker console app when and update is found to update the files, this application gets the process ID from the parent via arguments and then calls WaitForExit(). What I wish to know is what would be the best way of then restarting that process, would simply calling Proc.Start() be the best way of doing this? Ex:

static void Main(string[] args)
{
    if (args.Length == 2)
    {
        int ParentID = Convert.ToInt16(args[1]);
        Process ParentProc = Process.GetProcessById(ParentID);
        ParentProc.WaitForExit();
        Console.WriteLine(UpdateHandler.GetUpdates(
            Path.GetDirectoryName(ParentProc.MainModule.FileName), args[0]));
        ParentProc.Start();
    }
    else
        Console.WriteLine("Error: Invalid number of arguments.");
}

Or should I create a new process to launch it (I would assume using ParentProc.MainModule.FileName)

I would also like to know if there is a managed way to get the parent process not via arguments?

Thanks :)

Blam
  • 2,888
  • 3
  • 25
  • 39

2 Answers2

1

See How to get parent process in .NET in managed way

Community
  • 1
  • 1
Jerome
  • 1,162
  • 1
  • 6
  • 12
  • Thanks, don't know how I didn't see that. I would put this as the accepted answer, lacks answer to the main problem so: Upvote. – Blam Aug 27 '10 at 18:38
  • Nevermind, I just used a workaround, I'll set this as the accepted answer for trying to help :) – Blam Aug 27 '10 at 19:35
1

Ok nevermind, I did this by passing the executable path to the update program to act as both update path and process to relaunch. I did this for two reasons:

  1. The Solution linked to in Jerome's answer is slow, this way is faster.

  2. I had issues with 64bit and 32bit clashing when trying to get the file name via MainModule.FileName (even though I had set both programs as x86)

Community
  • 1
  • 1
Blam
  • 2,888
  • 3
  • 25
  • 39