我试过编写简单的WinForm应用程序,我的行为有问题。 目的是在面板上绘制两个可选颜色(红色和黑色)的小矩形。有4个按钮 - 每种颜色配对 - 绘制红色,停止红色,绘制黑色,停止黑色。
点击绘图按钮后,会生成 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);
}
答案 0 :(得分:0)
问题的根源在于,如果您单击运行任务的按钮3次,则您将使用3种不同的取消令牌源运行3个任务。
由于RunDrawingTask
中的这一行,您将覆盖刚刚传递给RunDrawingTask
的取消令牌
cnTokenSource = new CancellationTokenSource();
如果您删除该行,则每次都会传递相同的令牌,并且您的所有任务都可以对取消作出反应。
答案 1 :(得分:0)
您的逻辑对您的要求无效。解决方案之一:
您需要创建两个CancelationTokenSources:redCancelationTokenSource
和blackCancelationokenSource
。并重写RunDrawingTask
方法,如下所示:
private void RunDrawingTask(Pen pen, out CancellationTokenSource cnTokenSource) { int height = panel1.Height; int width = panel1.Width; Random random = new Random(); Rectangle rectangle = InitRectangleWidthAndHeights(); var cts = pen == blackPen ? blackTokenSource : redTokenSource; Task.Factory.StartNew(() => DrawRectangles(height, width, random, rectangle, pen, cts.Token), cts.Token); } CancellationTokenSource blackTokenSource = new CancellationTokenSource(); CancellationTokenSource redTokenSource = new CancellationTokenSource(); Pen blackPen = new Pen(Color.Black); Pen redPen = new Pen(Color.Red);
当你想要停止绘制动作时,你必须在CancelationTokenSources之一上调用'Cancel'方法:redCancelationSource或blackCancelationSource。