Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create background process in windows without visible console window

How do I create a background process with Haskell on windows without a visible command window being created?

I wrote a Haskell program that runs backup processes periodically but every time I run it, a command window opens up to the top of all the windows. I would like to get rid of this window. What is the simplest way to do this?

like image 989
airportyh Avatar asked Sep 09 '08 00:09

airportyh


2 Answers

You should really tell us how you are trying to do this currently, but on my system (using linux) the following snippet will run a command without opening a new terminal window. It should work the same way on windows.

module Main where
import System
import System.Process
import Control.Monad

main :: IO ()
main = do
  putStrLn "Running command..."
  pid <- runCommand "mplayer song.mp3" -- or whatever you want
  replicateM_ 10 $ putStrLn "Doing other stuff"
  waitForProcess pid >>= exitWith
like image 118
Alasdair Avatar answered Nov 16 '22 01:11

Alasdair


Thanks for the responses so far, but I've found my own solution. I did try a lot of different things, from writing a vbs script as suggested to a standalone program called hstart. hstart worked...but it creates a separate process which I didn't like very much because then I can't kill it in the normal way. But I found a simpler solution that required simply Haskell code.

My code from before was a simple call to runCommand, which did popup the window. An alternative function you can use is runProcess which has more options. From peeking at the ghc source code file runProcess.c, I found that the CREATE_NO_WINDOW flag is set when you supply redirects for all of STDIN, STOUT, and STDERR. So that's what you need to do, supply redirects for those. My test program looks like:

import System.Process
import System.IO
main = do
  inH <- openFile "in" ReadMode
  outH <- openFile "out" WriteMode
  runProcess "rsync.bat" [] Nothing Nothing (Just inH) (Just outH) (Just outH)

This worked! No command window again! A caveat is that you need an empty file for inH to read in as the STDIN eventhough in my situation it was not needed.

like image 5
airportyh Avatar answered Nov 16 '22 01:11

airportyh