Rupesh
Rupesh

Reputation: 1043

Run console application in background until I exit from it

I have a windows service which monitors the file system upon starting it. Now I need to have a similar console application which should run in background. I have ported the code to the console application but I need to control this application manually from any external program.

MyExternalProgram
{
    Start(Myconsole)
    // Do some operations.
    Exit(MyConsole)
}

Similar to the above. Any input?

Upvotes: 0

Views: 1867

Answers (2)

sloth
sloth

Reputation: 101032

If you're fine with just closing the console application, the simpliest way is to use Process.CloseMainWindow

Closes a process that has a user interface by sending a close message to its main window.

So call your console application with a Process and close it afterwards:

Dim p = new Process()
p.StartInfo.FileName = "c:\your\path\to\application.exe"
p.Start()
' p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden ' hide window
'
' do your stuff
'
p.CloseMainWindow()
' p.Kill() ' If window is hidden, you have to use Kill()

If you do not want to show the window of this application, use p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden, but then you have to stop it with Process.Kill, which will immediately stop the process.

Another way is to use some kind of IPC to send an exit signal to your console application, but I assume this would be overkill for your case.

Upvotes: 1

Jason Hughes
Jason Hughes

Reputation: 778

You can launch the app with a SHELL function in VB. You can set to minimized with a parameter:

Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus

However this would merely start a instance of your app. If some other program sent arguments to the app in a shell of its own or command line, that in most circumstances would be a separate instance (and PID).

Upvotes: 0

Related Questions