生成事件时暂停任务



我具有MS图表控件。我创建一个单独的线程以用点填充它,在绘制每个点后,我将线程入睡一段时间,然后绘制下一个点,以使图看起来像它的移动。这是代码。

Task[] t = new Task[1];
t[0] = Task.Factory.StartNew(() => plotChartPoints());

   public void plotPoint(int x, double y, int series)
    {
        comparisonChart.Series[series].Points.AddXY(x, y);
    }
  public void refreshChart()
    {
        this.mainSplitContainer.Panel2.Refresh();
    }
    public void plotChartPoints()
    {
        //comparisonChart.Series[0].Points.DataBindXY(xValuesSeries1.ToArray(), yValuesSeries1.ToArray());
        //comparisonChart.Series[1].Points.DataBindXY(xValuesSeries2.ToArray(), yValuesSeries2.ToArray());
        for (int index = 0; index < xValuesSeries1.Count; index++)
        {
            if (comparisonChart.InvokeRequired)
            {
                comparisonChart.Invoke(new MethodInvoker(() => plotPoint(xValuesSeries1.ElementAt(index), yValuesSeries1.ElementAt(index), 0)));
                comparisonChart.Invoke(new MethodInvoker(() => plotPoint(xValuesSeries2.ElementAt(index), yValuesSeries2.ElementAt(index), 1)));
            }
            Thread.Sleep(50);
            if (this.mainSplitContainer.InvokeRequired)
            { 
                mainSplitContainer.Invoke(new MethodInvoker(()=> refreshChart()));
            }
        }
    }

现在,我想添加一个按钮,以便单击按钮时,填充图表暂停的任务和图表冻结。我该如何完成?我正在使用.NET 4.0,我看不到任何方法可以暂停Task类中的任务

虽然您可以暂停任务正在运行的线程,但它比您已经使用的Sleep()还差。

您应该用Winforms计时器替换所有这些。这消除了对Invoke()和Sleep()和计时器的需求(Enabled = false;)。

您是否可以使用AutoResetEvent MSDN

然后在按钮上单击,您可以向绘图发出信号暂停。我假设再次单击按钮时,您将采取"脱节"操作。

有关更多控制,请查看ManualResetEventSlim MSDN

没有内置函数可以在任务或线程中执行此操作,只有OS调度程序才能暂停并恢复线程一旦启动。但是工作可能是将comcellationToken用作参数

TaskFactory.StartNew<TResult> Method (Func<TResult>, CancellationToken)

单击按钮并将索引的当前值存储在方法范围变量中,当您单击简历时,您可以从达到点

启动索引

遵循一个简单的示例,如何做

 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ChartController _chartController = new ChartController();
        public bool paused; 
        public MainWindow()
        {
            InitializeComponent();
        }
        private void PlayPauseButton_OnClick(object sender, RoutedEventArgs e)
        {
            paused = !paused;
            if (!paused)
            {
                _chartController.CancelAnim();
            }
            else
            _chartController.StartTask();
        }
    }
    public class ChartController
    {
        private CancellationTokenSource tokenSource = new CancellationTokenSource();
        private CancellationToken _cancellationToken;
        private int reachedChartIndex = 0;
        public void CancelAnim()
        {
            tokenSource.Cancel();
        }
        public ChartController()
        {
        }
        public void StartTask()
        {
            Task t = Task.Factory.StartNew(() => plotChartPoints(), tokenSource.Token);
            _cancellationToken = tokenSource.Token;

            //to handle exeption if there is 
            try
            {
                t.Wait();
            }
            catch (AggregateException e)
            {
                foreach (var v in e.InnerExceptions) 
                //here manage task exception 
            }
        }

        public void plotPoint(int x, double y, int series)
        {
            comparisonChart.Series[series].Points.AddXY(x, y);
        }
        public void refreshChart()
        {
            this.mainSplitContainer.Panel2.Refresh();
        }
        public void plotChartPoints()
        {
            _cancellationToken.ThrowIfCancellationRequested();
            //comparisonChart.Series[0].Points.DataBindXY(xValuesSeries1.ToArray(), yValuesSeries1.ToArray());
            //comparisonChart.Series[1].Points.DataBindXY(xValuesSeries2.ToArray(), yValuesSeries2.ToArray());
            for (int index = reachedChartIndex; index < xValuesSeries1.Count; index++)
            {
                if (_cancellationToken.IsCancellationRequested)
                {
                    reachedChartIndex = index;
                    break;
                }
                if (comparisonChart.InvokeRequired)
                {
                    comparisonChart.Invoke(
                        new MethodInvoker(
                            () => plotPoint(xValuesSeries1.ElementAt(index), yValuesSeries1.ElementAt(index), 0)));
                    comparisonChart.Invoke(
                        new MethodInvoker(
                            () => plotPoint(xValuesSeries2.ElementAt(index), yValuesSeries2.ElementAt(index), 1)));
                }
                Thread.Sleep(50);
                if (this.mainSplitContainer.InvokeRequired)
                {
                    mainSplitContainer.Invoke(new MethodInvoker(() => refreshChart()));
                }
            }
        }
    }
}

一个解决方法不会暂停线程,而是使用thread.sleep基于ui线程设置和重置的变量(或属性)。

    ...
        Thread.Sleep(50);//the sleep that you have in your code
        while (paused)
        {
        Thread.Sleep(100);
        }
    ...

最新更新