C# terminates the dormant Task

As shown in the figure, the above command will be executed every 10800000 milliseconds. What should I do if I want to terminate in the middle? The best way is to slice the time into every 2 seconds, or a few seconds, to shorten the execution sleep time, instead of directly letting the thread sleep for 10800000 milliseconds, refer to the following solution:

using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Text.RegularExpressions;
using System. Threading;
using System. Threading. Tasks;
using System. Windows;
using System. Windows. Controls;
using System. Windows. Data;
using System. Windows. Documents;
using System. Windows. Input;
using System. Windows. Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System. Windows. Navigation;
using System. Windows. Shapes;
using static System.Net.Mime.MediaTypeNames;

namespace WpfAppVPN_active
{<!-- -->
 
    public partial class MainWindow : Window
    {<!-- -->
        static bool isRun = false;

        /// <summary>
        /// The remaining survival time of the task, in milliseconds
        /// </summary>
        static int live_millseconds = 0;

        public MainWindow()
        {<!-- -->
            InitializeComponent();
        }

        /// <summary>
        /// run
        /// </summary>
        public void RunTask()
        {<!-- -->
            isRun = true;
            Task task = new Task(() =>
            {<!-- -->
                while (isRun)
                {<!-- -->
                    ShowMsg($"Task {<!-- -->Thread.CurrentThread.ManagedThreadId} will be executed after {<!-- -->live_millseconds} milliseconds...");
                    if (live_millseconds > 0)
                    {<!-- -->
                        goto TO_SLEEP;
                    }
                DO_TASK:
                    //executed command
                    string urlExe = string. Empty;

                    Dispatcher.Invoke(() =>
                    {<!-- -->
                        urlExe = txtUrl. Text;
                    });
                    try
                    {<!-- -->
                        string exe = urlExe. Split(' ')[0];
                        string args = urlExe.Split(new char[] {<!-- --> ' ' }, StringSplitOptions.RemoveEmptyEntries).Length > 1 ?
                        urlExe.Split(new char[] {<!-- --> ' ' }, StringSplitOptions.RemoveEmptyEntries)[1] : "";
                        if (string.IsNullOrWhiteSpace(args))
                        {<!-- -->
                            System.Diagnostics.Process.Start(exe);
                        }
                        else
                        {<!-- -->
                            System.Diagnostics.Process.Start(exe, args);
                        }
                        Dispatcher.InvokeAsync(() =>
                        {<!-- -->
                            string msg = $"{<!-- -->Thread.CurrentThread.ManagedThreadId} executed successfully";
                            ShowMsg(msg);
                        });
                    }
                    catch (Exception ex)
                    {<!-- -->
                        var ex1 = ex. InnerException  ex;
                        string msg = "Error: " + ex1.Message;
                        ShowMsg(msg);
                    }

                TO_SLEEP:
                    if (live_millseconds > 2000)
                    {<!-- -->
                        Thread. Sleep(2000);
                        live_millseconds -= 2000;
                    }
                    else
                    {<!-- -->
                        try
                        {<!-- -->
                            Thread. Sleep(live_millseconds);

                            Dispatcher.Invoke(() =>
                            {<!-- -->
                                // reset time to live
                                if (int. TryParse(txtTimeSpan. Text. Trim(), out live_millseconds))
                                {<!-- --> }
                            });

                            if (isRun)
                            {<!-- -->
                                //Start executing the command
                                goto DO_TASK;
                            }
                        }
                        catch (Exception ex)
                        {<!-- -->
                            var ex1 = ex. InnerException  ex;
                            ShowMsg("DO_TASK exception," + ex1.Message);
                        }
                    }
                }
                ShowMsg($"Task{<!-- -->Thread.CurrentThread.ManagedThreadId}End");
                Dispatcher.InvokeAsync(() => {<!-- -->
                    btnStart.IsEnabled = true;
                });
            }, TaskCreationOptions. LongRunning);
            task. Start();
        }

        private static object obj = new object();

        // OK, start
        private void Button_Click(object sender, RoutedEventArgs e)
        {<!-- -->
            //isRun = false;
            lock (obj)
            {<!-- -->
                if (!isRun)
                {<!-- -->
                    //Interval time, milliseconds
                    if (int. TryParse(txtTimeSpan. Text. Trim(), out live_millseconds))
                    {<!-- -->
                        btnStart. IsEnabled = false;
                        ShowMsg("Begin ");
                        RunTask();
                    }
                }
            }
        }

        //stop
        private void Stop_Click(object sender, RoutedEventArgs e)
        {<!-- -->
            if (isRun)
            {<!-- -->
                btnStart. IsEnabled = false;
                isRun = false;
                live_millseconds = 0;
                ShowMsg("Stopping...");
            }
        }
 
 
        /// <summary>
        /// display message
        /// </summary>
        /// <param name="msg"></param>
        private void ShowMsg(string msg)
        {<!-- -->
            if (string.IsNullOrWhiteSpace(msg))
            {<!-- -->
                return;
            }
            string msg2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "," + msg;
            Dispatcher.Invoke(new Action(() =>
            {<!-- -->
                listBox1.Items.Add(msg2);
                listBox1.ScrollIntoView(msg2);
            }));

        }
    }
}