none
background worker repeating dowork? RRS feed

  • Question

  • hi can anyone see what I am doing wrong with some code from my prime button checker on my scientific calculator. the backgroundworker seems to repeat the dowork section over and over as I click ok in each messagebox comes up, another comes straight up after that one closes, and the progressbar goes up 1% or less with each messagebox closed... ive fiddled around with it but cant get it to work :( thanks is advance....

    public static class PrimeTool
     {
         public static bool IsPrime(int primes)
         {
             // Test whether the parameter is a prime number.
             if ((primes & 1) == 0)
             {
                 if (primes == 2)
                 {
                     MessageBox.Show(primes + " Is Prime!");
    
                }
                 else
                 {
                     MessageBox.Show(primes + " Is Not Prime!");
                 }
             }
    
            for (int k = 3;
                 (k * k) <= primes; k += 2)
             {
                 if ((primes % k) == 0)
                 {
                     MessageBox.Show(primes + " Is Not Prime!");
                 }
             }
             return false;
         }
     }
    
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
     {
         BackgroundWorker worker = sender as BackgroundWorker;
         for (int i = 1; (i <= 100); i++)
         {
             primes = Convert.ToInt32(textBox1.Text);
             primes = int.Parse(textBox1.Text);
             PrimeTool.IsPrime(primes);
             backgroundWorker1.ReportProgress(i);
         }
     }
     private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
     {
         progressBar1.Value = e.ProgressPercentage;
         DateTime start = DateTime.Now;
         TimeSpan timespent = DateTime.Now - start;
         int timeleft = (timespent.Milliseconds / progressBar1.Value * (progressBar1.Maximum - progressBar1.Value));;
         textBox4.Text = progressBar1.Value.ToString();
         textBox3.Text = timeleft.ToString();
     }
    


    Thursday, September 28, 2017 7:48 PM

All replies

  • Seems like a great time to set break-points, step through the code to determine what is going on.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, September 28, 2017 9:14 PM
    Moderator
  • Hello Matrixreality,

    The DoWork method is  executed only once. You should put the code executed only once out of loop statement . Something like below.

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
    
                BackgroundWorker worker = sender as BackgroundWorker;
    
                int primes = Convert.ToInt32(textBox1.Text);
                PrimeTool.IsPrime(primes);
    
                for (int i = 1; (i <= 100); i++)
                {
                    backgroundWorker1.ReportProgress(i);
                   
                }
       }

    If you have any issues , please feel free to contact me.

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 29, 2017 5:25 AM
    Moderator
  • There is a fundamental flaw in your BWC DoWork method. Inside that method you are referencing the textBox1 UI element. You cannot access a UI element outside the UI thread that created it. The DoWork method runs outside the UI thread. Referencing that element should trigger an exception while running in the debugger with Managed Debug Assistants turned on (the default). Outside the debugger your program will eventually crash at odd locations because you've corrupted the data.

    All data needed by the DoWork method needs to be passed into the method. The DoWorkEventArgs parameter has the data passed to the DoWork method. You cannot touch the UI anywhere inside this method. The ProgressChanged and RunCompleted event handlers run on the UI and can update the UI accordingly but that wouldn't help in this case.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, September 29, 2017 2:04 PM
    Moderator