Maciek
Maciek

Reputation: 523

Waiting till process end

Hello i have to write program which have to open a few system properties like:

Process sound = new Process();
sound.StartInfo.FileName = "mmsys.cpl";
sound.Start();

// Place 1

Process device = new Process();
device.StartInfo.FileName = "hdwwiz.cpl";
device.Start();

// Place 2

// Other Code doing sth

And that works well, but my problem is that i must on first run mmsys.cpl, wait for user check what he must to do, and after closing window run hdwwiz.cpl.

So in // Place 1 i wrote:

sound.WaitForExit();

But that dont works, because mmsys.cpl is only shortcut and run as process "explorer.exe", and hdwwiz.cpl runs as "mmc.exe", so that comand doesnt wait till closing that windows, and run both at once.

Is any way to make sth like i want ?

Upvotes: 3

Views: 1178

Answers (2)

jlyonsmith
jlyonsmith

Reputation: 911

What you are running are called control panel files. Legacy ones, like mmsys.cpl are hosted in a processed with the name rundll32.exe You could get poll the list of these processes using:

Process[] processes = Process.GetProcessessByName("rundll32.exe");

and call WaitForExit on these Process objects. You can do the same for newer mmc.exe based .CPL's.

Obviously, this is not a great solution if there are multiple control panel applets running at the same time. You could perhaps put up a warning to the user close the other ones in this case.

Other more complicated solutions might involve using pinvoke to call the Win32 function EnumWindows to get a list of top level windows and hook them to watch for the WM_CLOSE message.

Upvotes: 1

Maciek
Maciek

Reputation: 523

Ok for properties like mmsys.cpl solution is:

Process sound = new Process();
sound.StartInfo.FileName = "rundll32.exe";
sound.StartInfo.Arguments = "shell32.dll,Control_RunDLL mmsys.cpl";
sound.Start();

sound.WaitForExit();

because that windows is runs via rundll32 command not explorer.exe, so i can check that process.

But still dont know how to open device manager. i found 2 commands:

  • devmgmt.msc
  • hdwwiz.cpl

but both run and create second process which i cannot track. Any solution for that ?

Upvotes: 0

Related Questions