【发布时间】:2016-01-03 03:10:25
【问题描述】:
我尝试编写简单的 WinForm 应用程序,但它的行为有些问题。 目的是在面板上用两种可选颜色(红色和黑色)绘制小矩形。有 4 个按钮 - 每种颜色对 - 画红色、停止红色、画黑色、停止黑色。
当点击Draw按钮时,会生成DrawRectangles的新任务。 当停止按钮被点击时,任务将被取消,使用 CancellationTokenSource。
问题是:当我创建了许多任务(即绘制红色的 3 个任务)之后,我按下停止按钮,它只取消了第一个任务,而我无法取消其他任务(绘制红色任务' ) 然后他们跑得没完没了。
以下代码:
public partial class Form1 : Form
{
CancellationTokenSource cnDrawRedToken;
CancellationTokenSource cnDrawBlackToken;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e{}
private void panel1_Paint(object sender, PaintEventArgs e){}
private void RedBtn_Click(object sender, EventArgs e)
{
RunDrawingTask(SetSpecificPen(Color.Red),out this.cnDrawRedToken);
}
private async Task DrawRectangles(int height, int width, Random random, Rectangle rectangle, Pen blackPen, CancellationToken cnToken)
{
while(true)
{
if (cnToken.IsCancellationRequested)
return;
rectangle.x = random.Next(0, width);
rectangle.y = random.Next(0, height);
this.panel1.CreateGraphics().DrawRectangle(blackPen, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
//Thread.Sleep(200);
await Task.Delay(150);
}
}
private Pen SetSpecificPen(Color color)
{
Pen blackPen = new Pen(color, 2);
return blackPen;
}
private Rectangle InitRectangleWidthAndHeights()
{
Rectangle rectangle = new Rectangle();
rectangle.width = 10;
rectangle.height = 10;
return rectangle;
}
private void StpRedBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawRedToken != null)
{
this.cnDrawRedToken.Cancel();
this.cnDrawRedToken = null;
}
else
{
this.cnDrawRedToken = new CancellationTokenSource();
}
}
private void BlackBtn_Click(object sender, EventArgs e)
{
RunDrawingTask(SetSpecificPen(Color.Black),out this.cnDrawBlackToken);
}
private void StpBlkBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawBlackToken != null)
{
this.cnDrawBlackToken.Cancel();
this.cnDrawBlackToken = null;
}
}
private void RunDrawingTask(Pen specificPen, out CancellationTokenSource cnTokenSource)
{
int height = this.panel1.Height;
int width = this.panel1.Width;
Random random = new Random();
Rectangle rectangle = InitRectangleWidthAndHeights();
cnTokenSource = new CancellationTokenSource();
CancellationTokenSource cts = cnTokenSource;
Task t = Task.Factory.StartNew(() => DrawRectangles(height, width, random, rectangle, specificPen, cts.Token), cts.Token);
}
}
我想要做的是当我点击停止按钮时,所有相同颜色的正在运行的任务都会被取消。
更新: 正如法比奥建议的那样,我重写了我的点击方法以及受影响的方法。之后,它可以按需要工作。
public Form1()
{
InitializeComponent();
cnDrawRedToken = new CancellationTokenSource() ;
cnDrawBlackToken = new CancellationTokenSource();
}
private void RedBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawRedToken.IsCancellationRequested)
{
this.cnDrawRedToken = null;
this.cnDrawRedToken = new CancellationTokenSource();
}
RunDrawingTask(SetSpecificPen(Color.Red), this.cnDrawRedToken);
}
private void StpRedBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawRedToken != null)
{
this.cnDrawRedToken.Cancel();
}
}
private void BlackBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawBlackToken.IsCancellationRequested)
{
this.cnDrawBlackToken = null;
this.cnDrawBlackToken = new CancellationTokenSource();
}
RunDrawingTask(SetSpecificPen(Color.Black), this.cnDrawBlackToken);
}
private void StpBlkBtn_Click(object sender, EventArgs e)
{
if (this.cnDrawBlackToken != null)
{
this.cnDrawBlackToken.Cancel();
}
}
private void RunDrawingTask(Pen specificPen, CancellationTokenSource cnTokenSource)
{
int height = this.panel1.Height;
int width = this.panel1.Width;
Random random = new Random();
Rectangle rectangle = InitRectangleWidthAndHeights();
Task t = Task.Factory.StartNew(() => DrawRectangles(height, width, random, rectangle, specificPen, cnTokenSource.Token), cnTokenSource.Token);
}
【问题讨论】:
标签: c# winforms task-parallel-library