Home > Net >  Background Worker RunWorkerCompleted isn't being called after DoWork finished
Background Worker RunWorkerCompleted isn't being called after DoWork finished

Time:06-28

I'm creating a simple program that pings all the servers on our network and returns whether the ping requests were successful.

I'm trying to utilise background workers so that the user can press the ping button and the pings run in the background while they can do other things on the UI

DoWork runs fine, there's no loop to keep it there infinitely, and it reaches the line:

r = pinger.Send(s)

and then from my understanding it ends and so the RunWorkCompleted method should be called?

I'm relearning programming after a long abscense so if I missed something obvious I apologise ...

   public Form1()
    {

        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.DoWork  = new DoWorkEventHandler(backgroundWorker1_DoWork);
    }

    private void Ping_Btn_Click(object sender, EventArgs e)
    {

        count = Convert.ToInt32(pingSeconds_TxtBox.Text);

        if (backgroundWorker1.IsBusy != true)
        {
            // Start operation
            backgroundWorker1.RunWorkerAsync();
        }
       
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        if(worker.CancellationPending == true)
        {
            e.Cancel = true;
        }
        else
        {
            for(int i = 0; i <= count; i  )
            {
                MessageBox.Show("something is happening");
                // Create ping object
                Ping pinger = new Ping();
                PingReply r;
                // IP to test ping
                string s = "###";

                try
                {
                    r = pinger.Send(s);

                }
                catch (Exception b)
                {
                    MessageBox.Show(b.ToString());
                }
            }
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Show me something");

        if(e.Cancelled == true)
        {
            statusLbl1.Text = "Cancelled";
        } else if(e.Error != null)
        {
            statusLbl1.Text = "Error: "   e.Error.Message;
        } else
        {
            statusLbl1.Text = "YEEEEEEEET";
        }
    }

...

CodePudding user response:

You need to attach your backgroundWorker1_RunWorkerCompleted event handler to the RunWorkerCompleted event. The C# compiler doesn't hook handlers to events based on naming conventions. You have to do it explicitly.

public Form1()
{
    InitializeComponent();
    backgroundWorker1.WorkerReportsProgress = true;
    backgroundWorker1.WorkerSupportsCancellation = true;
    backgroundWorker1.DoWork  = backgroundWorker1_DoWork;
    backgroundWorker1.RunWorkerCompleted  = backgroundWorker1_RunWorkerCompleted;
}

CodePudding user response:

If you want to keep an infinite loop, then you have to make a loop in your backgroundWorker1_DoWork Method. Something like this

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = (BackgroundWorker) sender;
    while (!worker.CancellationPending)
    {
        //Do your stuff here
        for(int i = 0; i <= count; i  )
        {
            MessageBox.Show("something is happening");
            // Create ping object
            Ping pinger = new Ping();
            PingReply r;
            // IP to test ping
            string s = "###";

            try
            {
                r = pinger.Send(s);

            }
            catch (Exception b)
            {
                MessageBox.Show(b.ToString());
            }
        }
    }        
 }

Also, it is not a good idea to display message boxes from your background thread, Log it in console or any file.

  • Related