目前的答案是您确实必须使用preLaunchTasks 才有机会完成这项工作。如果确实可行,我会很乐意使用丑陋的脚本-但事实并非如此。就我而言,我需要在后台运行一个或多个可执行文件,以允许 VSCode 继续调试。
不幸的是,我尝试启动的每个可执行文件(通过start)实际上并没有作为“分离”进程运行,因此 VSCode 会等待每个可执行文件完成运行,然后才能完成 preLaunchTasks 并开始调试.不是我想要的。
我找到了an article by someone having a similar "detached process" problem with subversion,并使用他的 C++ 代码解决了与 Visual Studio Code 相同的问题。我在该代码中发现了一两个错误,我已修复。这是我目前正在使用的:
// dstart.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
//http://stackoverflow.com/questions/1536205/running-another-program-in-windows-bat-file-and-not-create-child-process
//http://svn.haxx.se/users/archive-2008-11/0301.shtml
int _tmain()
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156(v=vs.85).aspx
LPWSTR pCmd = ::GetCommandLine();
// skip the executable
if (*pCmd++ == L'"')
while (*pCmd++ != L'"');
else
while (*pCmd != NULL && *pCmd != L' ') ++pCmd;
while (*pCmd == L' ') pCmd++;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
BOOL result = CreateProcess
(
NULL, // No module name (use command line)
pCmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set bInheritHandles to FALSE
CREATE_NEW_CONSOLE, // Detach process
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (returned)
);
if (result) return 0;
wchar_t msg[2048];
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
msg, sizeof(msg),
NULL
);
fputws(msg, stderr);
_flushall();
return -1;
}
编译后,您可以像在 DOS 提示符下使用 start 命令一样使用它。将其放在您附加到preLaunchTasks 的脚本中
在 Visual Studio Code 中。