Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Cancel background Worker c sharp

Status
Not open for further replies.

Ilia Gildin

Junior Member level 3
Joined
Sep 27, 2014
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
184
hello
I have the following code
Code:
        private void button26_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button8.Enabled = false;
            button14.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button9.Enabled = false;
            button10.Enabled = false;
            button11.Enabled = false;
            button12.Enabled = false;
            button17.Enabled = false;
            button13.Enabled = false;
            button19.Enabled = false;
            button20.Enabled = false;
            button18.Enabled = false;
            button23.Enabled = false;
            button16.Enabled = false;
            button15.Enabled = false;
            button21.Enabled = false;
            button24.Enabled = false;
            button22.Enabled = false;
            button25.Enabled = false;
            button26.Enabled = false;
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }
        }

Code:
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //NOTE: we shouldn't use a try catch block here (unless you rethrow the exception)  
            //the backgroundworker will be able to detect any exception on this code.  
            //if any exception is produced, it will be available to you on   
            //the RunWorkerCompletedEventArgs object, method backgroundWorker1_RunWorkerCompleted  
            //try  
            //{  
            DateTime start = DateTime.Now;
            int y = Convert.ToInt16(textBox28.Text);
            Int16 g = Convert.ToInt16(textBox27.Text);
            int i = y;
            while (i > 0)
                    {
                        if ((backgroundWorker1.CancellationPending == true))
                        {
                            e.Cancel = true;
                            break;
                        }
                        byte[] b = new byte[1] { Convert.ToByte(textBox13.Text) };
                        byte[] bytesToSend = new byte[3] { 0x4D, 0x54, 0X55 };
                        byte[] rv = new byte[bytesToSend.Length + 1];
                        System.Buffer.BlockCopy(bytesToSend, 0, rv, 0, bytesToSend.Length);
                        System.Buffer.BlockCopy(b, 0, rv, bytesToSend.Length, b.Length);
                        byte[] rw = new byte[rv.Length + 1];
                        System.Buffer.BlockCopy(rv, 0, rw, 0, rv.Length);
                        System.Buffer.BlockCopy(EOF, 0, rw, rv.Length, EOF.Length);
                        toolStripStatusLabel1.Text = BitConverter.ToString(rw);
                        Console.WriteLine(BitConverter.ToString(rw));
                        if ((backgroundWorker1.CancellationPending == true))
                        {
                            e.Cancel = true;
                            break;
                        }
                        //this.serialPort1.Write(rw, 0, rw.Length);
                        string s;
                        //s = this.serialPort1.ReadLine();
                        //s = s.TrimEnd('\r', '\n');
                        // label13.Text = s;
                        //if cancellation is pending, cancel work.  
                        if ((backgroundWorker1.CancellationPending == true))
                        {
                            e.Cancel = true;
                            break;
                        }
                        System.Threading.Thread.Sleep(g);
                        byte[] b1 = new byte[1] { Convert.ToByte(textBox14.Text) };
                        byte[] bytesToSend1 = new byte[3] { 0x4D, 0x54, 0X44 };
                        byte[] rv1 = new byte[bytesToSend1.Length + 1];
                        System.Buffer.BlockCopy(bytesToSend1, 0, rv1, 0, bytesToSend1.Length);
                        System.Buffer.BlockCopy(b1, 0, rv1, bytesToSend1.Length, b1.Length);
                        byte[] rw1 = new byte[rv1.Length + EOF.Length];
                        System.Buffer.BlockCopy(rv1, 0, rw1, 0, rv1.Length);
                        System.Buffer.BlockCopy(EOF, 0, rw1, rv1.Length, EOF.Length);
                        Console.WriteLine(BitConverter.ToString(rw1));
                        toolStripStatusLabel1.Text = BitConverter.ToString(rw1);
                        //this.serialPort1.Write(rw1, 0, rw1.Length);
                        string s1;
                        //s1 = this.serialPort1.ReadLine();
                        // s1 = s1.TrimEnd('\r', '\n');
                        //  label13.Text = s1; 
                        i = i - 1;
                        if ((backgroundWorker1.CancellationPending == true))
                        {
                            e.Cancel = true;
                            break;
                        }
                    }

                }

Code:
        private void backgroundWorker1_RunWorkerCompleted(object sender,
    RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("The task has been cancelled.");
                button2.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button8.Enabled = true;
                button14.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button9.Enabled = true;
                button10.Enabled = true;
                button11.Enabled = true;
                button12.Enabled = true;
                button17.Enabled = true;
                button13.Enabled = true;
                button19.Enabled = true;
                button20.Enabled = true;
                button18.Enabled = true;
                button23.Enabled = true;
                button16.Enabled = true;
                button15.Enabled = true;
                button21.Enabled = true;
                button24.Enabled = true;
                button22.Enabled = true;
                button25.Enabled = true;
                button26.Enabled = true;
            }
            else if (e.Error != null)
            {
                MessageBox.Show("Error.");
                button2.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button8.Enabled = true;
                button14.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button9.Enabled = true;
                button10.Enabled = true;
                button11.Enabled = true;
                button12.Enabled = true;
                button17.Enabled = true;
                button13.Enabled = true;
                button19.Enabled = true;
                button20.Enabled = true;
                button18.Enabled = true;
                button23.Enabled = true;
                button16.Enabled = true;
                button15.Enabled = true;
                button21.Enabled = true;
                button24.Enabled = true;
                button22.Enabled = true;
                button25.Enabled = true;
                button26.Enabled = true;
            }
            else
            {
                MessageBox.Show("The task has been completed.");
                button2.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button8.Enabled = true;
                button14.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button9.Enabled = true;
                button10.Enabled = true;
                button11.Enabled = true;
                button12.Enabled = true;
                button17.Enabled = true;
                button13.Enabled = true;
                button19.Enabled = true;
                button20.Enabled = true;
                button18.Enabled = true;
                button23.Enabled = true;
                button16.Enabled = true;
                button15.Enabled = true;
                button21.Enabled = true;
                button24.Enabled = true;
                button22.Enabled = true;
                button25.Enabled = true;
                button26.Enabled = true;
            }

        }

Code:
        private void button27_Click(object sender, EventArgs e)
        {
            //notify background worker we want to cancel the operation.  
           //this code doesn't actually cancel or kill the thread that is executing the job.
            backgroundWorker1.CancelAsync();
            
        }

        }

and my problem is that no meter how much I will press the button27 it wont stop until the while loop is finished and then I get the The task has been completed message. the backgroundworker1 settings are Generate member = true, Modifiers = private, WorkersReportsProgress = true,
WorkersSuportsCancellation = true, Dowork = backgroundWorker1_DoWork,
RunWorkerCompleted = backgroundWorker1_RunWorkerCompleted
I dont activate the back ground worker anywhere else so what can be my problem?
p.s
I wont paste my whole program here as it takes 1000 lines
please help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top