Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Process.Start problems...

Process.Start problems...

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpwinformsquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan van den Baard
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • J Jan van den Baard

      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

      J Offline
      J Offline
      Javier Lozano
      wrote on last edited by
      #2

      I'm having trouble understanding your question. Specially this part: _This all works except when the process has actually started the winforms app puts itself in front of the process which has just started_ Could you clarify? Thanks! ~javier lozano (blog || email)

      J 1 Reply Last reply
      0
      • J Javier Lozano

        I'm having trouble understanding your question. Specially this part: _This all works except when the process has actually started the winforms app puts itself in front of the process which has just started_ Could you clarify? Thanks! ~javier lozano (blog || email)

        J Offline
        J Offline
        Jan van den Baard
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • J Jan van den Baard

          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

          P Offline
          P Offline
          partyganger
          wrote on last edited by
          #4

          A call to BringToFront() or Focus() after calling Process.Start should do the trick.

          J 1 Reply Last reply
          0
          • P partyganger

            A call to BringToFront() or Focus() after calling Process.Start should do the trick.

            J Offline
            J Offline
            Jan van den Baard
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups