none
How to run a .exe in background from VB.net application ?

    Question

  •  

    Hi,

     

    I am trying to call a .exe from a VB.net application. I did this using System.Diagnostics.Process.Start(" my file name.exe"),this works but its UI also appears in the screen .Is there a way by which I can start the process and not let the user see it ,i.e to say can we call a .exe in the backdround?

     

    Please advise.

     

    Thanks and regards,

    Monday, March 31, 2008 6:41 AM

Answers

  • Yes you can hide the window.  There are several ways of doing this depending on the application.  It is very easy to do if its a console applicaiton.  You can just set the CreateNoWindow property in the process startinfo.  If you want to hide a window applicaiton it can it can be a little bit tricky.  You can try setting the window style on the start info to hidden but its up to the other application to honor that.  There is some unmanged calls you can make to to hide the window after the application has already started.  Check out this thread as the person has a simular problem.  Some code example from the other thread are below too.

    http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3032413&SiteID=1



    public enum ShowWindowType:int

    {

    Hide = 0 ,

    Minimized= 1 ,

    Maximized=2 ,

    Restore= 9

    }

    [DllImport("User32")]

    private static extern int ShowWindow(IntPtr handle, ShowWindowType hideType);

     

    private static void Test()

    {

    string _File = @"C:\windows\system32\notepad.exe";

    Process _Process = new Process();

    _Process.StartInfo.FileName = _File;

    _Process.Start();

    ShowWindow(_Process.MainWindowHandle, ShowWindowType.Hide);

    }



    private static void TestTwo()

    {

    string _File = @"C:\windows\system32\notepad.exe";

    Process _Process = new Process();

    _Process.StartInfo.FileName = _File;

    _Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    _Process.Start();

    }


    Monday, March 31, 2008 7:18 AM
  • You should use Windows Service to run background process.  Even if you can hide the application, the application is still using the UI thread as main thread.  Windows Service does not have UI.  It is used by many famous application running on the background, e.g. MS SQL, IIS, anti-virus programs, etc.  For information about building a Windows Service, please see my blog at http://www.biswaretech.com/blog3.aspx.

     

    Monday, March 31, 2008 8:00 AM

All replies

  • Yes you can hide the window.  There are several ways of doing this depending on the application.  It is very easy to do if its a console applicaiton.  You can just set the CreateNoWindow property in the process startinfo.  If you want to hide a window applicaiton it can it can be a little bit tricky.  You can try setting the window style on the start info to hidden but its up to the other application to honor that.  There is some unmanged calls you can make to to hide the window after the application has already started.  Check out this thread as the person has a simular problem.  Some code example from the other thread are below too.

    http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3032413&SiteID=1



    public enum ShowWindowType:int

    {

    Hide = 0 ,

    Minimized= 1 ,

    Maximized=2 ,

    Restore= 9

    }

    [DllImport("User32")]

    private static extern int ShowWindow(IntPtr handle, ShowWindowType hideType);

     

    private static void Test()

    {

    string _File = @"C:\windows\system32\notepad.exe";

    Process _Process = new Process();

    _Process.StartInfo.FileName = _File;

    _Process.Start();

    ShowWindow(_Process.MainWindowHandle, ShowWindowType.Hide);

    }



    private static void TestTwo()

    {

    string _File = @"C:\windows\system32\notepad.exe";

    Process _Process = new Process();

    _Process.StartInfo.FileName = _File;

    _Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    _Process.Start();

    }


    Monday, March 31, 2008 7:18 AM
  • You should use Windows Service to run background process.  Even if you can hide the application, the application is still using the UI thread as main thread.  Windows Service does not have UI.  It is used by many famous application running on the background, e.g. MS SQL, IIS, anti-virus programs, etc.  For information about building a Windows Service, please see my blog at http://www.biswaretech.com/blog3.aspx.

     

    Monday, March 31, 2008 8:00 AM
  • Hi Abdel,

     

    Your code worked for me . Thanks a lot

    Monday, March 31, 2008 10:30 AM
  • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            On Error Resume Next
            Dim p As System.Diagnostics.Process
            p = New System.Diagnostics.Process
           
            p.StartInfo.FileName = Shell("Notepad", AppWinStyle.Hide)

         ' Set the Window style to AppWinStyle.MaximizedFocus
            p.Start()
          
        End Sub

    Thursday, July 07, 2011 2:10 PM