Process.Start problems...
-
I have a C# winforms app which starts other processes using System.Diagnostics.Process.Start. This all works except when the process has actually started the winforms app puts itself in front of the process which has just started. Is there a way to suppress this behaviour? Thanks in advance... We are the all singing, all dancing crap of the world. - Tyler Durden
-
I have a C# winforms app which starts other processes using System.Diagnostics.Process.Start. This all works except when the process has actually started the winforms app puts itself in front of the process which has just started. Is there a way to suppress this behaviour? Thanks in advance... We are the all singing, all dancing crap of the world. - Tyler Durden
-
If the winforms app opens for example a Word document the sequence is as follows: 1) It opens the Word application. 2) The document is loaded in Word. 3) The winforms app main window put's itself in front of the Word main window. In other words it seems that the winforms app does some sort of "SetForegroundWindow()" after the System.Diagnostics.Process.Start() returns. Thanks, Jan We are the all singing, all dancing crap of the world. - Tyler Durden
-
If the winforms app opens for example a Word document the sequence is as follows: 1) It opens the Word application. 2) The document is loaded in Word. 3) The winforms app main window put's itself in front of the Word main window. In other words it seems that the winforms app does some sort of "SetForegroundWindow()" after the System.Diagnostics.Process.Start() returns. Thanks, Jan We are the all singing, all dancing crap of the world. - Tyler Durden
A call to BringToFront() or Focus() after calling Process.Start should do the trick.
-
A call to BringToFront() or Focus() after calling Process.Start should do the trick.
Further investigation has shown to me that the problem does not happen always. If I use Process.Start() to open a word or Excel document it does happen but if I open a text file it does not. When the problem occures the following happens: 1) Call Process.Start("somefile.doc"); 2) The form receives a Form_Activated() event. I should tell that the documents are opened via clicking in a treeview in which these files are listed. I have found "sort of" a solution to the problem which pertains to calling SetForegroundWindow() on the MainWindowHandle of the created process when possible/necessary. Resulting in something like shown below. Sucks big time but it does the trick for now...
[DllImport("user32")]
private static extern IntPtr SetForegroundWindow( IntPtr hWnd );
[DllImport("user32")]
private static extern int GetAsyncKeyState( long vKey );// prProc is the initiated process...
private void MainForm_Activated(object sender, System.EventArgs e)
{
try
{
// A mouse button pressed? If so presume the window was
// activated by the user.
if (( GetAsyncKeyState( 0x01 /* VK_LBUTTON */ ) & ( 1 << 15 )) == ( 1 << 15 ) ||
( GetAsyncKeyState( 0x02 /* VK_RBUTTON */ ) & ( 1 << 15 )) == ( 1 << 15 ) ||
( GetAsyncKeyState( 0x04 /* VK_MBUTTON */ ) & ( 1 << 15 )) == ( 1 << 15 ))
{
// Will not be needed anymore...
prProc = null;
}
else if ( prProc != null )
{
// Process still running?
if ( prProc.HasExited == false )
// Put it's window upfront.
SetForegroundWindow( prProc.MainWindowHandle );
// No longer necessary.
prProc = null;
}
}
catch ( InvalidOperationException )
{
// We did a SetForgroundWindow() when the process died
// if we get here...
}
}Thanks... We are the all singing, all dancing crap of the world. - Tyler Durden