I need to show the Windows Task Scheduler window and wait until the user closes it before continuing the main program.
I've tried with this method:
public static bool RunCommand (string command)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Verb = "runas",
Arguments = "/C " + command,
UseShellExecute = false
};
Process process = new();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
return true;
}
catch (Exception)
{
return false;
throw;
}
}
I've tried passing the following commands as the argument "command" in order to show up the Task Scheduler:
The idea is to continue the execution of the main program when the Task Scheduler is closed (its process is killed), but the "process.WaitForExit();" instruction is linked to the cmd command, so the main program continues after cmd process is closed, even when the Task Scheduler window is still being displayed.
The problem is that the "taskschd.msc" process, when launched, is wrapped into an "svchost.exe" process. I can't determine its PID because of the many different svchosts running at the same time, so I can't kill it manually either.
I've tried the solutions proposed here, but to no avail.
I've also been looking for a way to give an alias to the the "scvhost.exe" process launched with this cmd, but it seems that's not possible.
I've been thinking of compiling a new exe just to launch the Task Scheduler under a process name that I could control, but I don't think it is a good solution.
Any ideas, please?
Launching taskschd.msc through mmc.exe works:
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = "mmc.exe",
Arguments = "taskschd.msc",
Verb = "runas",
UseShellExecute = true,
CreateNoWindow = true
};
var process = Process.Start(psi);
process.WaitForExit();