如何中止/取消转发的BackgroundWorker?

时间:2014-12-20 15:27:27

标签: c# stream backgroundworker

正如您所见,here有一个ftp-transfer的后台工作人员 弗兰克斯的答案显示了一个新的课程,其中包含一些重要的方法,因此可以获得传输速度和百分比。

我想取消运行上传的背景工作者,但我不知道如何
实施

public UpAndDownloadForm(Job pJob)
    {
        InitializeComponent();
        this.job = pJob;
        bGroundWorker = new BackgroundWorker();
        bGroundWorker.WorkerReportsProgress = true;
        bGroundWorker.WorkerSupportsCancellation = true;

        bGroundWorker.DoWork += new DoWorkEventHandler(bGroundWorker_DoWork);
        bGroundWorker.ProgressChanged += new ProgressChangedEventHandler(bGroundWorker_ProgressChanged);
        bGroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bGroundWorker_RunWorkerCompleted);

        bGroundWorker.RunWorkerAsync(pJob);
    }

DoWork的:

void bGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        streamWithTransferSpeedWorker = (BackgroundWorker)sender;
        Job job = (Job)e.Argument;
        string[] files = Directory.GetFiles(job.GetSourceFolder(), "*.*", SearchOption.AllDirectories);

        if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
        {
            e.Cancel = true;
            return;
        }
        foreach (string file in files)
        {
            char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
            Array.Reverse(temp); //Reihenfolge ändern (von hinten nach vorne)
            string fileReversed = new string(temp); //als string speichern
            int index = fileReversed.IndexOf(@"\"); //die Zeichenlänge des DateiNAMENs auslesen
            string fileOnlyName = file.Substring(file.Length - index, index); //nur den Dateinamen auslesen


            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(job.GetDestinationFolder() + "\\" + fileOnlyName);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(Manager._user, Manager._password);


            var response = (FtpWebResponse)request.GetResponse(); //WebResponse response = request.GetResponse(); 

            using (Stream source = File.OpenRead(file))
            {
                using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), streamWithTransferSpeedWorker, source.Length))//request.ContentLength))
                {
                    source.CopyTo(destination);
                }
            }

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();
        }
    }

取消BackgroundWorker:

private void btnCancelLoad_Click(object sender, EventArgs e)
    {
        if (bGroundWorker.IsBusy)
        {
            bGroundWorker.CancelAsync();
        }
        if (streamWithTransferSpeedWorker.IsBusy)
        {
            streamWithTransferSpeedWorker.CancelAsync();
        }
    }


正如你所看到的,我已经尝试了一些东西,但它没有任何意义 后台工作人员CancellationPending - 在按钮点击事件后开始上传的标记为true,但上传仍在运行。

我想念一些东西吗? 在写这个问题的过程中,我考虑过取消CopyTo - 方法,但我甚至不知道这是否有效或是否是问题/解决方案。
感谢帮助。

<小时/> 的 EDIT1:
在这里,您可以看到转发的BackgroundWorker using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), streamWithTransferSpeedWorker, source.Length))

<小时/> 的 EDIT2:
正如 T MCKeown 所说:bool实例 的 UploadForm.cs:

private void btnCancelLoad_Click(object sender, EventArgs e)
    {
        if (bGroundWorker.IsBusy)
        {
            bGroundWorker.CancelAsync();
            Manager._cancelBackgroundWorker = true;
        }
    }

另外我的部分:
StreamWithTransferSpeed.cs:

if (Manager._cancelBackgroundWorker)
{
    this.worker.CancelAsync(); //this is useless, because the Backgroundworker is still running
    Manager._cancelBackgroundWorker = false;
    innerStream.Close(); //this is dirty, but it works, after the Closing the backgroundworker is canceled
}

在这里你可以看到CancelAsync()。它是在Console.WriteLine("bGW direkt unter der exception abgebrochen");处执行的:
UploadForm.cs:

void bGroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;
        Job job = (Job)e.Argument;
        string[] files = Directory.GetFiles(job.GetSourceFolder(), "*.*", SearchOption.AllDirectories);

        foreach (string file in files)
        {
            if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
            {
                e.Cancel = true; //Sinnlos da es eh nicht funktioniert
                Console.WriteLine("bGW direkt unter foreach abgebrochen");
                return;
            }
            char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
            Array.Reverse(temp); //Reihenfolge ändern (von hinten nach vorne)
            string fileReversed = new string(temp); //als string speichern
            int index = fileReversed.IndexOf(@"\"); //die Zeichenlänge des DateiNAMENs auslesen
            string fileOnlyName = file.Substring(file.Length - index, index); //nur den Dateinamen auslesen

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(job.GetDestinationFolder() + "\\" + fileOnlyName);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(Manager._user, Manager._password);

            var response = (FtpWebResponse)request.GetResponse(); //WebResponse response = request.GetResponse(); 
            try
            {
                using (Stream source = File.OpenRead(file))
                {
                    using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), worker, source.Length))//request.ContentLength))
                    {
                        source.CopyTo(destination);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(ObjectDisposedException))
                {
                    Console.WriteLine("Upload canceled.");
                    if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
                    {
                        e.Cancel = true; //Sinnlos da es eh nicht funktioniert
                        Console.WriteLine("bGW direkt unter der exception abgebrochen");
                        return;
                    }
                }                    
            }

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();
        }
    }

2 个答案:

答案 0 :(得分:2)

创建_cancel

类型的实例bool变量
private void btnCancelLoad_Click(object sender, EventArgs e)
{
  _cancel = true;
  ...
}

然后在这里查看:

foreach (string file in files)
    {
       if (_cancel ) return;

        char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
        ...

将if语句添加到您可能要检查取消的任何其他位置。

对EDIT2的回复:

您已将代码更改为:

foreach (string file in files)
    {
        if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
        {
            e.Cancel = true; //Sinnlos da es eh nicht funktioniert
            Console.WriteLine("bGW direkt unter foreach abgebrochen");
            return;
        }

我建议你做这样的事情:

foreach (string file in files)
    {
        if (Manager._cancelBackgroundWorker) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
        {
            e.Cancel = true; //Sinnlos da es eh nicht funktioniert
            Console.WriteLine("bGW direkt unter foreach abgebrochen");
            return;
        }

单击取消是,将此布尔值设置为true

答案 1 :(得分:0)

Cancel检查代码放在Foreach循环中

if (bGroundWorker.CancellationPending) //Falls der bGWorker abgebrochen wurde, beendet man diesen wohl einfach mit e.Cancel;
{
    e.Cancel = true;
    return;
}