如何在向进度更改事件报告 5% 后取消后台工作者操作

本文关键字:取消 取消后 后台 操作 工作者 事件报告 | 更新日期: 2023-09-27 18:35:13

在dowork事件中,我做了:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var root = Convert.ToString(e.Argument);  // txtHost.Text;
            searched_until_now_dirs = 0;
            total_dirs = 1;
            max_percentage = 0;
            last_update = DateTime.Now;
            directories_real_time = new TreeNode();
            var dirNode = CreateDirectoryNode(root, "root",1);
            e.Result = dirNode;
        }

然后在进度更改事件中:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.toolStripStatusLabel1.Text = e.UserState.ToString();
            this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage);
            label11.Text = numberOfFiles.Count.ToString();
            if (e.ProgressPercentage == 5)
            {
                backgroundWorker1.CancelAsync();
            }
        }

首先,如果它是 5,它永远不会进入它继续下去。永远不要到达取消异步行。我希望在完成 5% 的工作后,它将被取消并进入已完成的事件。

这是我在CreateDirectoryNode方法中做进度报告的地方:

private int total_dirs;
        private int searched_until_now_dirs;
        private int max_percentage;
        private TreeNode directories_real_time;
        private string SummaryText;
        private TreeNode CreateDirectoryNode(string path, string name , int recursive_levl )
        {
            var directoryNode = new TreeNode(name); to its Tag
            var directoryListing = GetDirectoryListing(path);
            var directories = directoryListing.Where(d => d.IsDirectory);
            var files = directoryListing.Where(d => !d.IsDirectory);
            total_dirs += directories.Count<FTPListDetail>();
            searched_until_now_dirs++;
            int percentage;
            foreach (var dir in directories)
            {
                directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name, recursive_levl+1));

                if (recursive_levl == 1)
                {
                    TreeNode temp_tn = (TreeNode)directoryNode.Clone();
                    this.BeginInvoke(new MethodInvoker( delegate
                    {
                        UpdateList(temp_tn);
                    }));
                }

                percentage = (searched_until_now_dirs * 100) / total_dirs;
                if (percentage > max_percentage)
                {
                    SummaryText = String.Format("Searched dirs {0} / Total dirs {1}", searched_until_now_dirs, total_dirs);
                    max_percentage = percentage;
                    backgroundWorker1.ReportProgress(percentage, SummaryText);
                }
            }
            percentage = (searched_until_now_dirs * 100) / total_dirs;
            if (percentage > max_percentage)
            {
                SummaryText = String.Format("Searched dirs {0} / Total dirs {1}", searched_until_now_dirs, total_dirs);
                max_percentage = percentage;
                backgroundWorker1.ReportProgress(percentage, SummaryText);
            }
            foreach (var file in files)
            {
                TreeNode file_tree_node = new TreeNode(file.Name);
                file_tree_node.Tag = "file" ; // you can put here any object you want..
                directoryNode.Nodes.Add(file_tree_node);
                numberOfFiles.Add(file.FullPath);
            }

            return directoryNode;
        }

如何在向进度更改事件报告 5% 后取消后台工作者操作

我没有看到您向工人报告进度。下面的示例应该涵盖您的案例。

    public Form1()
    {
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;
        if (e.ProgressPercentage >= 5)
        {
            worker.CancelAsync();
        }
    }
    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;
        while(!worker.CancellationPending)
        {
            // TODO: do something.
            // TODO: update percentProgress 
            // backgroundWorker.ReportProgress(percentProgress);
        }
        if (worker.CancellationPending)
        {
            e.Cancel = true;
        }
    }