I have an MS chart control. I create a separate thread to populate it with points and after plotting each point, I put the thread to sleep for some time then plot the next point, so that the graph looks like its moving. Here is the code.
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()));
}
}
}
Now, I want to add a button so that when the button is clicked the task which is populating the chart pauses and the chart freezes.
How do I accomplish this? I am using .NET 4.0 and I don't see any method to pause a Task in the Task
class