Control the start, stop and restart of tasks

I want to create a looping connection check task (that is started after connection are started) both these function work but I have no clue on how to restart a task that was previously executed. Basically this is what i want to achieve:

create task loop

I can only start tasks directly, any attempt to create a task and execute it later failed. Also restarting a previously executed task fails. Can anyone help me, is it possible what I want to achieve? if so is this the right direction or am i doing something structural wrong? This is bugging me for days, any help or tips would be appreciated.

class Program
{       
    static void Main(string[] args)
    {       
        Task<ReturnResult> ConnectionStartResult = CommManager.StartAllConnections();
        Task<List<ReturnResult>> ConnectionCheckResult = CommManager.CreateConnectionCheckTask();
        //Task<List<ReturnResult>> ConnectionCheckResult = CommManager.StartConnectionCheck();

        ConnectionStartResult.ContinueWith((prevTask) =>
        {
            CommManager.ValueListerStart();
            //ConnectionCheckResult.Start();
        });

        ConnectionCheckResult.ContinueWith((myPreviusTask) =>
        {
            CommManager.ValueListerStop();
            Console.WriteLine("Connection problems found");
            ConnectionStartResult = CommManager.StartAllConnections();
        });
    }
}


public class CommManager
{
    private static CancellationTokenSource CommCheckTokenSource;

    // starts connections in the background, end when done
    public static async Task<ReturnResult> StartAllConnections()
    {
        ReturnResult result = await Task.Run(() => ConnectAll());
        return result;
    }

    private static ReturnResult ConnectAll()
    {
        Thread.Sleep(1000);
        return new ReturnResult();
    }

    // start and run connection check. Works, but i cannot recall this after connection problem 
    public static async Task<List<ReturnResult>> StartConnectionCheck()
    {
        CommCheckTokenSource = new CancellationTokenSource();
        var CommCheckToken = CommCheckTokenSource.Token;
        return await Task.Run(() => CheckPlcConnectionsTask(CommCheckToken));
    }

    // only create task. (but do not start) works but locks up the UI thread, whan started in main
    public static Task<List<ReturnResult>> CreateConnectionCheckTask()
    {
        CommCheckTokenSource = new CancellationTokenSource();
        var CommCheckToken = CommCheckTokenSource.Token;
        return new Task<List<ReturnResult>>(() => CheckPlcConnectionsTask(CommCheckToken));
    }        

    // the connection check function
    private static List<ReturnResult> CheckPlcConnectionsTask(CancellationToken token)
    {
        var returnList = new List<ReturnResult>();
        while (!token.IsCancellationRequested)
        {
            // keep checking connection only return when disconnect is found
            Thread.Sleep(3000);  // for test
            return returnList;
        }
        return returnList;
    }


    public static void ValueListerStart()
    { }

    public static void ValueListerStop()
    { }
}

public class ReturnResult
{ }
链接地址: http://www.djcxy.com/p/52806.html

上一篇: DateTime.Now更新频率如何? 或者是否有更精确的API来获取

下一篇: 控制任务的开始,停止和重新启动