62mkv
62mkv

Reputation: 1522

How to run background task in VS Code under Windows?

I have setup some scripts in package.json file, which should be run in background, like

{
  "scripts": {
    "babel-watch": "babel client -d _tmp/babel -w",
    "server": "node server.js",
    "background": "start npm run babel-watch && start npm run server"
  }
}

I would like to run the background task from the VS Code, so I set up tasks.json like this (some lines are omitted):

{
  "command": "npm",
  "args": [],
  "isShellCommand": true,
  "showOutput": "always",
  "suppressTaskName": true,
  "tasks": [
    {
      "taskName": "background",
      "args": ["run", "background"]
    }
  ]
}

If I run the npm run background from Far Manager, 2 new console windows are started and I am returned into Far. However, when I run it in VS Code (using task background, 2 new console windows are started, BUT VS code waits until the task completes, before it lets me execute another task (an error is displayed: There is an active running task right now. Terminate it first before executing another task. (and I am given an option "Terminate running task", which only leads to an error The launched process doesn't exist anymore. If the task spawned background tasks exiting VS Code might result in orphaned processes.). It is as though VS Code watches all the started subprocesses, or smth like that.

Is there a way to circumvent this behaviour and make VS Code "forget" about spawned background processes?

Upvotes: 2

Views: 10986

Answers (2)

ForNeVeR
ForNeVeR

Reputation: 6955

One tricky way I've discovered is as follows:

{
  "windows": {"command": "powershell"},
  "suppressTaskName": true,
  "tasks": [
    {
      "taskName": "test",
      "args": ["(New-Object -ComObject WScript.Shell).Run('notepad', 1, $false)"]
    }
  ]
}

That will start notepad.exe and it'll be completely separated from VS Code instance (e.g. you can start the same task again, and it won't complain).

Why does it work?

VS Code uses the standard node.js child_process module to control children processes (see processes.ts), and it'll by default watch child process' pipes' state. The pipes are derived by any start'ed process instance, that's how VS Code detects the whole process subtree state (and that's why your start trick won't work here).

One of the easiest ways to detach child process completely is to use WScript.Shell COM object, that's why I've chosen PowerShell for implementation. The same may be achieved with cscript and a small script file, or with small helper program that'll call Win32 API function CreateProcess with the DETACHED_PROCESS flag.

Pro Tip™: Replace 1 with 0 to run the process in a hidden window!

Upvotes: 5

Ashhar Hasan
Ashhar Hasan

Reputation: 912

You can't run multiple tasks at the same time right now. That's a limitation.

See this issue to track any developments in this field.

If you read the "Current Limitations" portion, the first explicitly says that we can only run a single task at a time right now.

Upvotes: 1

Related Questions