首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FluentFTP。如何停止下载过程

FluentFTP。如何停止下载过程
EN

Stack Overflow用户
提问于 2020-06-15 00:48:41
回答 1查看 543关注 0票数 0

我需要做一个停止下载文件的按钮。例如,我在下载开始和第二次停止时点击了1。

代码语言:javascript
复制
        private static async void Download()
    {
        foreach (string fileName in fileList)
        {

            string localDir = AppDomain.CurrentDomain.BaseDirectory;
            localDir.Substring(0, localDir.Length - 1);
            localDir += fileName;
            long fileSize = await ftp.GetFileSizeAsync(fileName);
            fileSize /= 1024;
            form.progressBar1.Maximum = (int)fileSize;
            var token = new CancellationToken();

            Progress<FtpProgress> progress = new Progress<FtpProgress>(async loadedFile =>
            {
                if (loadedFile.Progress == 100)
                {
                    form.progressBar1.Value = 0;
                }
                else
                {
                    int x = (int)fileSize * Convert.ToInt32(loadedFile.Progress) / 100;
                    string value = loadedFile.TransferSpeedToString();
                    form.label1.Text = "Connection Speed: \n" + value;

                    form.progressBar1.Value = x;

                }
            });
            await ftp.DownloadFileAsync(localDir, fileName, FtpLocalExists.Skip, FluentFTP.FtpVerify.Retry, progress, token);
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-15 01:01:53

首先,您不能直接创建CancellationToken,您要创建一个CancellationTokenSource并获取它的Token

说到这里,你可以想象一下使用那个令牌,来允许取消操作。你可以这样做:

代码语言:javascript
复制
//At class level
CancellationTokenSource cancel = null;

private static async void Download()
{

    if(cancel != null)
    {
        cancel.Cancel();
        cancel.Dispose();
        cancel = null;
        return;
    }

    cancel = new CancellationTokenSource();

    foreach (string fileName in fileList)
    {

        string localDir = AppDomain.CurrentDomain.BaseDirectory;
        localDir.Substring(0, localDir.Length - 1);
        localDir += fileName;
        long fileSize = await ftp.GetFileSizeAsync(fileName);
        fileSize /= 1024;
        form.progressBar1.Maximum = (int)fileSize;

        Progress<FtpProgress> progress = new Progress<FtpProgress>(async loadedFile =>
        {
            if (loadedFile.Progress == 100)
            {
                form.progressBar1.Value = 0;
                cancel.Dispose();
                cancel = null;
            }
            else
            {
                int x = (int)fileSize * Convert.ToInt32(loadedFile.Progress) / 100;
                string value = loadedFile.TransferSpeedToString();
                form.label1.Text = "Connection Speed: \n" + value;

                form.progressBar1.Value = x;

            }
        });


        try
        {

            await ftp.DownloadFileAsync(localDir, fileName, FtpLocalExists.Skip, FluentFTP.FtpVerify.Retry, progress, cancel.Token);

        }
        catch
        { 
           //When the download is cancelled will throw an exception
           //you can create a more specific handler
           cancel.Dispose();
           cancel = null;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62375505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档