Search code examples
c#streambackgroundworker

How can I abort/cancel the forwarded BackgroundWorker?


As you can see here there is a backgroundworker for the ftp-transfer.
Franks answer shows a new class with some overrided methods, so that it is possible to get the transfer speed and the percentage of it.

I want to cancel the backgroundworker which runs the upload, but I dont know how.

Implementation

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();
        }
    }

Cancel BackgroundWorker:

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


As you can see I already tried something, but it dont make any sense.
The Backgroundworker-CancellationPending-Flag for starting the upload is true after Button-Click-Event, but the Upload is still running.

Do I missunderstand something ?
During writing this question, I thought about canceling the CopyTo-Method, but I even dont know whether this works or whether this is the problem/solution.
Thanks for help.


EDIT1:
Here you can see the forwarded BackgroundWorker using (var destination = new StreamWithTransferSpeed(request.GetRequestStream(), streamWithTransferSpeedWorker, source.Length)).


EDIT2:
As T MCKeown said: instance of bool
UploadForm.cs:

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

and additionally my part:
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
}

and here you can see the CancelAsync(). It's executed at 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();
        }
    }

Solution

  • Create an instance _cancel variable of type bool

    private void btnCancelLoad_Click(object sender, EventArgs e)
    {
      _cancel = true;
      ...
    }
    

    Then check it here:

    foreach (string file in files)
        {
           if (_cancel ) return;
    
            char[] temp = file.ToCharArray(); //Dateipfad in CharArray speichern
            ...
    

    Add the if statement any where else you may want to check for cancel.

    RESPONSE to EDIT2:

    You changed your code to this:

    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;
            }
    

    I had suggested you do something like this:

    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;
            }
    

    You set this boolean to true upon clicking cancel yes?