c#简单任务取消

时间:2015-06-10 19:23:38

标签: c# task-parallel-library task

我无法阻止停止线程。 我尝试了100种方法。 怎么了 ? 这很刺激

(我必须在这里写一些文字,因为stackoverflow.com不允许我发这篇文章)(我必须在这里写一些文字因为stackoverflow.com不允许我发这篇文章)(我有在这里写一些文字,因为stackoverflow.com不允许我发送这篇文章)

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1977)
x, y, z = np.random.random((3, 50))

# Bin the data onto a 10x10 grid
# Have to reverse x & y due to row-first indexing
zi, yi, xi = np.histogram2d(y, x, bins=(10,10), weights=z, normed=False)
counts, _, _ = np.histogram2d(y, x, bins=(10,10))

zi = zi / counts
zi = np.ma.masked_invalid(zi)

fig, ax = plt.subplots()
ax.pcolormesh(xi, yi, zi, edgecolors='black')
scat = ax.scatter(x, y, c=z, s=200)
fig.colorbar(scat)
ax.margins(0.05)

plt.show()

1 个答案:

答案 0 :(得分:1)

只需使用此代替work_for_task方法:

private void work_for_task(CancellationToken ct)
{
    for (int i = 0; i < 5; i++)
    {
        if (ct.IsCancellationRequested)
        {
            return; 
        }
        if (richTextBox2.InvokeRequired)
        {
            richTextBox2.Invoke((MethodInvoker)delegate
            {
                richTextBox2.AppendText("I AM IN: " + Thread.CurrentThread.Name + "\n");
            });

            Thread.Sleep(1400);
        }
    }
}