I need to be able to start processes (both console and windowed) without it stealing focus. The only way within the .NET framework that I found to do this is Microsoft.Visu
The VB.Net team has done much more to ease things for the developer regarding instrumentation of windows, and I see no problem adding a reference to a VB dll and use that in your C# program.
It's two teams with different focus, that's all. And you shouldn't feel bad about using Microsoft.VisualBasic.Interaction.Shell if it solves your issue.
You can also use Reflector to see the actual implementation and implement the code yourself if you don't want to reference the dll.
[Edit - added code example after comment to show you can combine Interaction.Shell and Process]
int pid = Interaction.Shell("notepad.exe", AppWinStyle.NormalFocus);
Process p = Process.GetProcessById(pid);
p.Exited += ((o, e) => Console.WriteLine("Exit"));
p.EnableRaisingEvents = true;
Console.ReadLine();
Here I use the Shell method to kick off the process, get a handle to the process from the pid, and hook on events. You can even do p.Kill() in order to abort the process.
[Edit - workaround for cmd.exe]
It's starting to become a bit hackish to my taste, but it works. Replace "NEWWINDOW" with a random guid or something to make it unique.
Microsoft.VisualBasic.Interaction.Shell(@"cmd.exe /c ""start cmd.exe /k title NEWWINDOW""", AppWinStyle.NormalFocus);
foreach (var process in Process.GetProcessesByName("cmd"))
{
if (process.MainWindowTitle.EndsWith("NEWWINDOW"))
{
process.Exited += ((o, e) => Console.WriteLine("Exit"));
process.EnableRaisingEvents = true;
}
}
Have a look here:
System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo(); procInfo.CreateNoWindow = true; procInfo.UseShellExecute = true; procInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procInfo; proc.EnableRaisingEvents = true; proc.Exited += new EventHandler(proc_Exited); proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived); proc.Start(...) // Do something with proc.Handle... void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) { /* Do something here... */ } void proc_Exited(object sender, EventArgs e) { /* Do something here... */ }
Edit: I have modified the code to show the means of raising events and handling them, also, I have shown the usage of the Handle
property which is the handle of the process that is running.