Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
This code below I found on the Internet. This is a simple example of code how works BackgroundWorker, and it works well.
I need the code to Abort (stop) BackgroundWorker correctly. 
I want to know how it works. Thanks a lot.


C#
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            byte[] buffer;
            byte[] oldBuffer;
            int bytesRead;
            int oldBytesRead;
            long size;
            long totalBytesRead = 0;
          
            using (Stream stream = File.OpenRead((string) e.Argument))
            using (HashAlgorithm hashAlgorithm = MD5.Create())
            {
                size = stream.Length;

                buffer = new byte[4096];

                bytesRead = stream.Read(buffer, 0, buffer.Length);
                totalBytesRead += bytesRead;

                do
                {
                    oldBytesRead = bytesRead;
                    oldBuffer = buffer;

                    buffer = new byte[4096];
                    bytesRead = stream.Read(buffer, 0, buffer.Length);

                    totalBytesRead += bytesRead;

                    if (bytesRead == 0)
                    {
                        hashAlgorithm.TransformFinalBlock(oldBuffer, 0, oldBytesRead);
                    }
                    else
                    {
                        hashAlgorithm.TransformBlock(oldBuffer, 0, oldBytesRead, oldBuffer, 0);
                    }

                    BackgroundWorker.ReportProgress((int)((double)totalBytesRead * 100 / size));
                } while (bytesRead != 0);

                e.Result = hashAlgorithm.Hash;
            }
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StringBuilder sb;

            sb = new StringBuilder();

            ProgressPercentage.Visible = false;
            ProgressBar.Visible = false;
            StatusText.Text = "";

            foreach (byte b in (byte[]) e.Result)
            {
                sb.AppendFormat("{0:X2}", b);
            }
            HashTextbox.Text = sb.ToString();
            
        }

        private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ProgressBar.Value = e.ProgressPercentage;
            ProgressPercentage.Text = e.ProgressPercentage + "%";
        }


What I have tried:

if (backgroundWorker1.IsBusy == true)
{

backgroundWorker1.CancelAsync();
while (backgroundWorker1.IsBusy == true)
{
backgroundWorker1.CancelAsync();
}

backgroundWorker1.Dispose();

}

backgroundWorker1.RunWorkerAsync();
Posted
Updated 6-Aug-16 11:24am

1 solution

And, what happened when you tried your code ? Was there an error ? Did it not do something you expected it to ?

You need to study the documentation and example on MSDN: [^].

Set the Property 'WorkerSupportsCancellation of the BackgroundWorker to true, and check the 'CancellationPending Property inside the loop in your 'DoWork method; if 'CancellationPending is 'true set the e.Cancel EventArgs Property to 'true.

Call 'CancelAsynch to stop the Worker. The example on MSDN demonstrates everything you need to know.

Copying code you find on the web and not actually studying documentation, or searching CodeProject or StackOverFlow for examples, or tips, is a good way to make sure you do not progress in learning how to program.

Think how good you are going to feel as you begin to know you are actually mastering the power of the .NET and the C# language, how you are going to say to yourself: "well, that hard work was worth it." :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900