Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background worker continuously running issue c# [duplicate]

I have created a c# project using background worker. It contain a "Start" button for starting background worker. This is my code.

using System.Threading.Tasks;
using System.Windows;

namespace Test
{

    public partial class MainWindow : Window
    {

        int status = 0;

        private void btnstart_Click(object sender, RoutedEventArgs e)
        {

            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            Console.WriteLine("Background worker started successfully");
            btnsave.IsEnabled = false;
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
               Console.WriteLine("Status : " + status);
                if (status == 0)
                {
                    status = 1;
                }
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          Console.WriteLine("worker completed");
          btnsave.IsEnabled = true;
        }
    }
}

But every time, i have click on start button, i got output like this

attempt 1.

Status : 0
The thread 0x23dc has exited with code 0 (0x0).
Status : 1


attempt 2.

Status : 0
The thread 0x148 has exited with code 0 (0x0).
Status : 1
Status : 1

attempt 3.

Status : 0
The thread 0x128 has exited with code 0 (0x0).
Status : 1
Status : 1
Status : 1

I am starting the background worker only after the existing worker is completed. Then why is the "status" log is getting printed like this.

like image 222
Abdul Manaf Avatar asked Jan 19 '26 18:01

Abdul Manaf


1 Answers

The problem results from the event handler assignment:

worker.RunWorkerCompleted += worker_RunWorkerCompleted;

This is done every time you click the button. Since its a += you assign it multiple times.

Just add the event handler in the window initialization:

using System.Threading.Tasks;
using System.Windows;

namespace Test
{

    public partial class MainWindow : Window
    {

        int status = 0;

        // should be called when the window is loaded
        private void ApplicationStart()
        {
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }

        private void btnstart_Click(object sender, RoutedEventArgs e)
        {            
            worker.RunWorkerAsync();
            Console.WriteLine("Background worker started successfully");
            btnsave.IsEnabled = false;
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
               Console.WriteLine("Status : " + status);
                if (status == 0)
                {
                    status = 1;
                }
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          Console.WriteLine("worker completed");
          btnsave.IsEnabled = true;
        }
    }
}
like image 189
Romano Zumbé Avatar answered Jan 21 '26 07:01

Romano Zumbé