Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理来自不同类的CancellationToken_C#_Cancellation - Fatal编程技术网

C# 处理来自不同类的CancellationToken

C# 处理来自不同类的CancellationToken,c#,cancellation,C#,Cancellation,我有一门课是这样的: public class FtpTaskVideo : IFtpTask { //some fields public CancellationTokenSource tokenSource = new CancellationTokenSource(); private Panel CreatePanel(string text, int count, int value) { Panel pnlOutput = new

我有一门课是这样的:

public class FtpTaskVideo : IFtpTask
{
    //some fields
    public CancellationTokenSource tokenSource = new CancellationTokenSource();

    private Panel CreatePanel(string text, int count, int value)
    {
        Panel pnlOutput = new Panel();
        pnlOutput.Name = "pnlInfo";
        pnlOutput.AutoSize = true;
        pnlOutput.BorderStyle = BorderStyle.FixedSingle;

        //adding some controls

        Button btnUserCancel = new Button();
        btnUserCancel.Name = "btnUserCancel";
        btnUserCancel.AutoSize = true;
        btnUserCancel.Text = "Stop";
        btnUserCancel.Click += new EventHandler(btnUserCancel_Click);
        pnlOutput.Controls.Add(btnUserCancel);
        btnUserCancel.BringToFront();

        return pnlOutput;
    }

    public void btnUserCancel_Click(object sender, EventArgs e)
    {
        tokenSource.Cancel();
    }

    public void Start()
    {
         //some code
        while(somethingToDownload)
        {
         var task = Task<SharedConstants.downloadFtpFileStatus>.Factory.StartNew(() => dff.Download(tokenSource.Token), tokenSource.Token);
         try
         {
             downloadStatus = task.Result;
         }
         catch (System.AggregateException exc)
         {
               //do something                 
         }
         //some code
        }
}

现在,我有另一个类,dff是它的一个实例,Download是它的方法。dff所做的事情之一是根据下载方法操作期间获得的数据更新和重画面板。如何,在它绘制一个按钮并按下它之后,我可以将取消令牌发送回原始类以阻止其下载?

为什么需要将令牌发送回?
FtpTaskVideo
dff
似乎已经使用了相同的标记。@Enigmativity可能我不太理解它,因为这是我第一次使用它,但我的问题是:类dff有两种方法-下载和重画面板。下载接受CancellationToken作为参数。每当新下载开始时,就会调用RedrawPanel。当我按下面板上的按钮(由重画面板生成)时,我如何发送令牌以使其停止任何进一步的下载呼叫?除非您发布消息,否则我们无法回答此问题。我们需要所有相关的代码。你需要花时间创建一个简单的例子来说明你的问题。这有意义吗?
public Shared.Classes.SharedConstants.downloadFtpFileStatus Download(CancellationToken token)
{
    if (token.IsCancellationRequested)
    {
        return Shared.Classes.SharedConstants.downloadFtpFileStatus.CANCELLED;               
    }
    else //do some stuff
}