简体   繁体   English

如何使用tpl分别监视和停止运行任务

[英]How to monitor and stop running tasks separately using tpl

I am new to parallel programming and tpl in c# and I am doing the following test in order to see how tasks work. 我不熟悉并行编程和c#中的tpl,我正在进行以下测试,以便了解任务的工作原理。 Each time I click the button1 and the bounded method is fired, a new task will start. 每次单击button1并触发有界方法时,都会启动一个新任务。 When I click button_2 all tasks will exit the loop and I will get the time elapsed for all tasks and there task id. 当我单击button_2时,所有任务都将退出循环,我将获得所有任务和任务ID的时间。

So is there a way to monitor these tasks while they are working in a task pool so I can stop them if I wish separately using the Task Id? 那么有没有办法在他们在任务池中工作时监视这些任务,所以如果我希望单独使用任务ID,我可以阻止它们?

        Boolean stopprogramm = false;
        private void button1_Click(object sender, EventArgs e)
        {

            Task timerTask1 = Task.Factory.StartNew(() =>
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while (stopprogramm == false)
                    {
                        //Do some work
                    }
                    Console.WriteLine("time ellapsed for No partitioning at all version {0}ms , thread id was {1}", watch.ElapsedMilliseconds, Task.CurrentId);
                    watch.Stop();

                });


        }
        private void button2_Click(object sender, EventArgs e)
        {
            stopprogramm = true;
        }

Something like this could work. 像这样的东西可以工作。

    private Dictionary<Guid, TaskTracker> _taskMap = new Dictionary<Guid, TaskTracker>();

    private void OnButton1Click(object sender, EventArgs eventArgs)
    {
        TaskTracker taskTracker = new TaskTracker(Guid.NewGuid(), OnDoWork);
        _taskMap.Add(taskTracker.Identity, taskTracker);
        taskTracker.Run();
    }

    private void OnDoWork()
    {
        // Do some work
    }

    private void OnButton2Click(object sender, EventArgs eventArgs)
    {
        Guid identity = _taskMap.Count > 0 ? _taskMap.First().Value.Identity : default(Guid);  // find some way to get the desired task

        TaskTracker taskTracker;
        if (_taskMap.TryGetValue(identity, out taskTracker))
        {
            taskTracker.TaskExiting += OnTaskExiting;
            taskTracker.Stop();
        }
    }

    private void OnTaskExiting(object sender, EventArgs eventArgs)
    {
        TaskTracker taskTracker = (TaskTracker)sender;
        taskTracker.TaskExiting -= OnTaskExiting;
        _taskMap.Remove(taskTracker.Identity);
        // do what you want with the timings
    }

The class for task is this: 任务的类是这样的:

public class TaskTracker
{
    private readonly Action OnDoWork;
    private Task _task;
    private bool _isRunning = true;

    public TaskTracker(Guid identity, Action onDoWork)
    {
        Identity = identity;
        OnDoWork = onDoWork;
    }

    public readonly Guid Identity;
    public readonly Stopwatch Stopwatch = new Stopwatch();

    public event EventHandler TaskExiting;

    public void Run()
    {
        Task _task = Task.Factory.StartNew(
            () =>
                {
                    Stopwatch.Start();
                    try
                    {
                        while (_isRunning)
                        {
                            OnDoWork();
                        }
                        if (TaskExiting != null)
                        {
                            TaskExiting(this, EventArgs.Empty);
                        }
                    }
                    finally
                    {
                        Stopwatch.Stop();
                    }
                }
            );
    }

    public void Stop()
    {
        _isRunning = false;
        // wait for task to exit?
        _task = null;
    }
}

You will need to fill in the blanks, meaning tracking tasks and getting the target task when hitting button 2. 您需要填写空白,意味着跟踪任务并在按下按钮2时获取目标任务。

You mentioned in one of the comments from my other answer a link, it looks like that link describes how to make it cancellable. 您在我的其他答案中的一条评论中提到了一个链接,看起来该链接描述了如何使其可取消。 I have adapted my example to support this: 我已经调整了我的例子来支持这个:

    private Dictionary<Guid, TaskTracker> _taskMap = new Dictionary<Guid, TaskTracker>();

    private void OnButton1Click(object sender, EventArgs eventArgs)
    {
        TaskTracker taskTracker = new TaskTracker(Guid.NewGuid(), OnDoWork);
        _taskMap.Add(taskTracker.Identity, taskTracker);
        taskTracker.Start();
    }

    private void OnDoWork(CancellationToken token)
    {
        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(100); // Do some work, checking if cancel requested every once in a while
            if (token.IsCancellationRequested)
            {
                token.ThrowIfCancellationRequested();
            }
        }
    }

    private void OnButton2Click(object sender, EventArgs eventArgs)
    {
        Guid identity = _taskMap.Count > 0 ? _taskMap.First().Value.Identity : default(Guid);  // find some way to get the desired task

        TaskTracker taskTracker;
        if (_taskMap.TryGetValue(identity, out taskTracker))
        {
            taskTracker.TaskExiting += OnTaskExiting;
            taskTracker.Stop();
        }
    }

    private void OnTaskExiting(object sender, EventArgs eventArgs)
    {
        TaskTracker taskTracker = (TaskTracker)sender;
        taskTracker.TaskExiting -= OnTaskExiting;
        _taskMap.Remove(taskTracker.Identity);

        Console.WriteLine("Time ellapsed for No partitioning at all version {0}ms , thread id was {1}", taskTracker.Stopwatch.ElapsedMilliseconds, Task.CurrentId);
    }

    private void OnButton3Click(object sender, EventArgs eventArgs)
    {
        Guid identity = _taskMap.Count > 0 ? _taskMap.First().Value.Identity : default(Guid);  // find some way to get the desired task

        TaskTracker taskTracker;
        if (_taskMap.TryGetValue(identity, out taskTracker))
        {
            taskTracker.TaskExiting += OnTaskExiting;
            taskTracker.Cancel();
        }
    }

And here is the TaskTracker class: 这是TaskTracker类:

public class TaskTracker
{
    private readonly Action<CancellationToken> OnDoWork;
    private readonly CancellationTokenSource TokenSource;
    private readonly CancellationToken Token;

    private bool _isRunning;
    private Task _task;

    public TaskTracker(Guid identity, Action<CancellationToken> onDoWork)
    {
        TokenSource = new CancellationTokenSource();
        Token = TokenSource.Token;

        Identity = identity;
        OnDoWork = onDoWork;
    }

    public readonly Guid Identity;
    public readonly Stopwatch Stopwatch = new Stopwatch();

    public event EventHandler TaskExiting;

    public void Start()
    {
        _isRunning = true;
        _task = Task.Factory.StartNew(
            () =>
                {
                    Stopwatch.Start();
                    try
                    {
                        while (_isRunning)
                        {
                            OnDoWork(Token);
                        }
                    }
                    finally
                    {
                        Stopwatch.Stop();

                        if (TaskExiting != null)
                        {
                            TaskExiting(this, EventArgs.Empty);
                        }
                    }
                }, Token
            );
    }

    public void Stop(bool waitForTaskToExit = false)
    {
        if (_task == null)
        {
            throw new InvalidOperationException("Task hasn't been started yet");
        }

        _isRunning = false;
        if (waitForTaskToExit)
        {
            _task.Wait();
        }
        _task = null;
    }

    public void Cancel()
    {
        if (_task == null)
        {
            throw new InvalidOperationException("Task hasn't been started yet");
        }

        _isRunning = false;
        TokenSource.Cancel();
        _task = null;
    }
}

Basically, in your "Worker" you would check to see if the token indicates it is being cancelled, if so do some cleanup and then call the method ThrowIfCancellationRequested on token to break out. 基本上,在你的“工作者”中,你会检查令牌是否表明它被取消,如果是,那么做一些清理,然后调用令牌上的方法ThrowIfCancellationRequested爆发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM