您的問題的直接答案是,start /b
將讓你分叉另一個線程到同一個窗口,所以你可以在同一個窗口同時運行兩個指令。但是,問題在於分叉線程不再受按鍵或用戶輸入的影響。任何與start /b
一起發佈的東西都會在後臺運行,並且不再是互動的 - 因此,殺死它不是一個簡單的方法。您可以使用waitfor
在父線程和子線程之間建立一些通信,但這會變得非常複雜且難以維護。
我想你的choice
命令中使用/t
開關更好的解決方案。這樣,您可以在一個命令中同時擁有用戶輸入的計時器和監聽器。
您可以用作清除每個循環迭代的屏幕的替代方法是輸出回車符,以便將文本插入符號返回到行的開頭,並在等待時重複覆蓋該行用戶輸入。這樣可以讓您在線路末尾保留>
作爲提示,同時將點添加到LOADING...
,而無需在循環中包含cls
。
試試這個腳本,看看它是否提供了你想要創建的用戶體驗。
@echo off & setlocal
echo Press either A for call or B for server. Do it now.
set "str=LOADING."
rem // capture an extended character as a default option for "choice"
for /f %%I in ('forfiles /p "." /m "%~nx0" /c "cmd /c echo 0x99"') do set "char=%%~I"
rem // capture a carriage return to overwrite the same line
rem // credit: https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/AUXP8XbRAJA
for /f %%a in ('copy /Z "%~dpf0" nul') do set "\r=%%a"
:prompt
setlocal enabledelayedexpansion
rem // Output a carriage return + "Loading..." with increasing dots + ">" without new line
set /P "=.!\r!%str% > " <NUL
endlocal
rem // On timeout, default to a (probably) untypeable character. Add a dot and loop again
choice /t 1 /c ab%char% /d %char% /n /m "" >NUL
if errorlevel 3 (
set "str=%str%."
goto prompt
)
echo;
if errorlevel 2 goto server
rem // default is to go to :call
:call
echo I'm at call.
exit /b
:server
echo I'm at server.
exit /b
歡迎來到Stack Overflow。如果您發現我的回答有用,請考慮將其標記爲已接受。 [見本頁](https://meta.stackexchange.com/a/5235/275822),解釋爲什麼這很重要。 – rojo
[並行執行shell進程](https://stackoverflow.com/q/672719/995714),[等待並行批處理腳本](https://stackoverflow.com/q/12577442/995714),[如何並行運行多個DOS命令?](https://stackoverflow.com/q/11010834/995714) –