views:

129

answers:

3

I have a c# win forms program and after I click a button it launches a windows application and then when I'm done, I want to close the app and return to my win form. When I close the launched app, my win form always seems to get pushed all the way to the back and I have to hunt for it. Any ideas?

var process1 = new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;

var leftPath = @"c:\codereview\" + curDateTime + @"\left\";
var rightPath = @"c:\codereview\" + curDateTime + @"\right\";
var execPath = @"c:\program files\winmerge\winmergeu.exe";
var strCmdLine = "/C " + '"' + execPath + '"' + " /r " + leftPath + " " + rightPath;
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
+1  A: 

Try calling the Activate() method on your form. Note that depending on the OS, this might just flash the Form in the taskbar. If that's the case, you might have to resort to some interop to bring your app at the top of the windows stack.

Franci Penov
A: 

I think I don't really get what you are trying to do, looking at your code. You seem to create an empty process on one hand (process1), but then start another manually (by directly calling the static Process.Start method). The process1 reference isn't doing anything.

Try completely erasing the code referencing process1, and keeping just the part creating the cmdline and calling Process.Start:

var leftPath = @"c:\codereview\" + curDateTime + @"\left\";
var rightPath = @"c:\codereview\" + curDateTime + @"\right\";
var execPath = @"c:\program files\winmerge\winmergeu.exe";
var strCmdLine = "/C " + '"' + execPath + '"' + " /r " + leftPath + " " + rightPath;
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);

Please also note that the process is started asynchronously. Your code will continue to run directly after creating the cmd process, it will not wait for this process to finish.

M-Peror
+1  A: 

use process.WaitForExit() then this.Focus() // or this.Select()

And an other observation. Instead of using "a" + b + "c" + d use string.Format("a{0}c{0}", b, d) - this have better performance and is less time consuming.

serhio
Er, string.Format is <i>slower</i>: see http://blogs.msdn.com/ricom/archive/2004/03/12/88715.aspx. (This is a shame, I prefer it for all kinds of reasons.)
Jeremy McGee
@Jeremy: for time consumption is almost the same thing, as for the memory usage, the string.Format is better.
serhio
`"a" + b + "c" + d` will be rewritten by the compiler as `string.Concat("a", b, "c", d)` which results in a single allocation. And Rico said in his blog post that `string.Format` is *worse* in both memory consumption and speed.
Joey