Working column constantly running problem c #

I created a C # project using a desktop background. It contains a Start button to start the 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 click the start button I get an 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 only start working with a background worker after the existing one is finished. Then why the "status" log is printed like this.

+3


source to share


1 answer


The problem comes from the assignment of the event handler:

worker.RunWorkerCompleted += worker_RunWorkerCompleted;

      

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



Just add an event handler to your 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;
        }
    }
}

      

+7


source







All Articles