Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace ScheduledTask
- {
- class Program
- {
- static void Main(string[] args)
- {
- string taskName = "Your Scheduled Task Name";
- Process loteWinProcess = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = "CMD.exe",
- Arguments = @$"/C SCHTASKS.EXE /RUN /TN ""{taskName}""",
- UseShellExecute = false,
- RedirectStandardOutput = true,
- CreateNoWindow = true
- }
- };
- Console.WriteLine($"Started Task: {taskName}");
- loteWinProcess.Start();
- loteWinProcess.WaitForExit();
- WaitScheduledTaskBeDone(taskName);
- Console.WriteLine("Success!");
- Console.Beep();
- }
- public static void WaitScheduledTaskBeDone(string taskName, int delay = 1000)
- {
- string status = GetScheduledTaskStatus(taskName);
- while (status != "READY" && status != "PRONTO")
- {
- status = GetScheduledTaskStatus(taskName);
- Task.Delay(delay).Wait();
- }
- }
- public static string GetScheduledTaskStatus(string taskName)
- {
- Process scheduledTaskProcess = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = "SCHTASKS.exe",
- Arguments = @$"/Query /TN ""{taskName}"" /FO TABLE /NH",
- UseShellExecute = false,
- RedirectStandardOutput = true
- }
- };
- scheduledTaskProcess.Start();
- scheduledTaskProcess.WaitForExit();
- return Regex.Replace(scheduledTaskProcess.StandardOutput.ReadToEnd().ToUpper(), @"\t|\n|\r", "")
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .LastOrDefault();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement