Page 1 of 1

How to pause and resume a task?

Posted: 13 Jul 2022 15:26
by itsmetoo
Hi there!

Long story short:
I need to know how I can "pause" and "resume" a task with a batch file. I know this must be possible ... in a way. But I don't know how.
(Via Resource Monitor / Network I need to pause an Exe file and after a few seconds I need to resume that Exe. I thought, a batch file could be way easier than always to hit the Task Manager and going to Resource Monitor)
Another question would be, if possible, how to add a specific shortcut to a batchfile, so that I don't have to go to the batch file every time and doubleclick it. But that's another story then!

Thank you very much in advance, yours itsmetoo

Re: How to pause and resume a task?

Posted: 15 Jul 2022 00:17
by miskox
This might help:

net stop schedule in a batch would work, opposite would be net start schedule.

(see https://www.google.com/url?sa=t&rct=j&q ... xyk_V-1_AW)

Or maybe (if your .cmd is like this): you could add an if command to check if a certain file exists then it pauses* a .cmd and continues when this files is not there anymore

Saso

* pause in a way you check for this file again in a loop

Re: How to pause and resume a task?

Posted: 18 Jul 2022 10:56
by aGerman
Even if the OP didn't respond yet ...
itsmetoo wrote:
13 Jul 2022 15:26
I need to pause an Exe file and after a few seconds I need to resume that Exe
Based on that it seems that a process shall be suspended where the NET command wouldn't be applicable. There's the PsSuspend utility as part of Mark Russinovich's Sysinternals. With the aid of some PowerShell code it is also possible to achieve the suspension of a local process without additional utilities.

Code: Select all

@echo off &setlocal

:: *** macro definitions (PowerShell hybrid codes) *** ::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: suspend_proc - Suspend the execution of a process.
:: Syntax:
::   %suspend_proc% process_base_name
:: Argument:
::   process_base_name   Name of the process without the .exe extension.
:: Remarks:
::   - If more than one process with the same name is running, the suspension
::     will fail because the name can not unambiguously specify the process to
:      be suspended.
::   - It is possible to successively suspend a process multiple times. The code
::     of the suspend_proc macro does not check whether a process is already
::     suspended. To actually resume the process it is necessary to call the
::     resume_proc macro as many times as suspend_proc has been called.
::   - If successful, the suspend_proc macro returns exit code 0 to the calling
::     process. Exit code 1 is returned otherwise.
setlocal DisableDelayedExpansion
set suspend_proc=for %%- in (1 2) do if %%-==2 (powershell.exe -nop -ep Bypass -c ^"^
%===% $w=Add-Type -Name W32 -PassThru -MemberDefinition '[DllImport(\"ntdll.dll\")] public static extern bool NtSuspendProcess(IntPtr h);';^
%===% try{exit $w::NtSuspendProcess((gps ^^^^^^^!p^^^^^^^! -ea SilentlyContinue).handle)}catch{exit 1}^"^
%=% ^&endlocal) else setlocal EnableDelayedExpansion^&set p=
endlocal&set "suspend_proc=%suspend_proc%"
if !!# neq # set "suspend_proc=%suspend_proc%"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: resume_proc - Resume execution of a suspended process.
:: Syntax:
::   %resume_proc% process_base_name
:: Argument:
::   process_base_name   Name of the process without the .exe extension.
:: Remarks:
::   - If more than one process with the same name is running, the resumption
::     will fail because the name can not unambiguously specify the process to
::     be resumed.
::   - It is possible that a process was successively suspended multiple times.
::     To actually resume the process it is necessary to call the resume_proc
::     macro as many times as suspend_proc has been called.
::   - If successful, the resume_proc macro returns exit code 0 to the calling
::     process. Exit code 1 is returned otherwise.
setlocal DisableDelayedExpansion
set resume_proc=for %%- in (1 2) do if %%-==2 (powershell.exe -nop -ep Bypass -c ^"^
%===% $w=Add-Type -Name W32 -PassThru -MemberDefinition '[DllImport(\"ntdll.dll\")] public static extern bool NtResumeProcess(IntPtr h);';^
%===% try{exit $w::NtResumeProcess((gps ^^^^^^^!p^^^^^^^! -ea SilentlyContinue).handle)}catch{exit 1}^"^
%=% ^&endlocal) else setlocal EnableDelayedExpansion^&set p=
endlocal&set "resume_proc=%resume_proc%"
if !!# neq # set "resume_proc=%resume_proc%"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:: *** application example *** ::

:: create a charmap process just to demonstrate how it works
start "" charmap.exe
pause

:: suspend charmap
%suspend_proc% charmap
echo suspend_proc: errorlevel %errorlevel%
pause

:: resume charmap
%resume_proc% charmap
echo resume_proc: errorlevel %errorlevel%
pause

:: clean up
taskkill /im charmap.exe /f
Steffen

Re: How to pause and resume a task?

Posted: 19 Jul 2022 02:16
by penpen
Just a small sidenote to the net-approach:
The net command (at least under windows 10) does support the switches PAUSE and CONTINUE (to pause and resume a task).
You don't need to terminate and (re)start the process (using STOP and START).

penpen