我有一个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
类
答案 0 :(得分:2)
虽然您可以暂停线程,但任务正在运行,这将比您已经使用的Sleep()
更糟糕。
您应该使用WinForms Timer替换所有这些。这消除了对Invoke()和Sleep()的需要,并且可以轻松地停止Timer(Enabled = false;
)。
答案 1 :(得分:0)
在Task或线程中没有内置函数可以执行此操作OS调度程序可以在启动后暂停和恢复线程。但解决方法可能是使用CancellationToken作为参数
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()));
}
}
}
}
}
答案 2 :(得分:0)
你能否使用AutoResetEvent
MSDN
然后在按钮上单击,您可以指示绘图暂停。我假设再次点击按钮时你会有一个“取消暂停”动作。
要获得更多控制权,请查看ManualResetEventSlim
MSDN
答案 3 :(得分:0)
一种解决方法不是暂停线程,而是使用基于UI线程设置和重置的变量(或属性)的Thread.Sleep。
...
Thread.Sleep(50);//the sleep that you have in your code
while (paused)
{
Thread.Sleep(100);
}
...