0

I need to update my progressbar with EventHandler by a Class, but in the same form. How can i do that? Background work it's a way? I have tried to pass the progressbar parameter, but won't work.

class GenerateBackup()
void Backup()
{
   Backup bkp = new Backup(){..}
   bkp.PercentComplete += bkp_PercentComplete;
   bkp.Complete += bkp_Complete;
}

private void bkp_Complete (Label Status, object sender, ServerMessageEventArgs e)
{
    if (e.Error != null)
    {
        Status.Invoke((MethodInvoker)delegate
        {
            Status.Text = e.Error.Message;
        });
    }
}

private void bkp_PercentComplete(object sender, PercentCompleteEventArgs e)
{
    barraProgresso.Invoke((MethodInvoker)delegate
    {
        barraProgresso.Value = e.Percent;
        barraProgresso.Update();
    });

    lblPercentual.Invoke((MethodInvoker)delegate
    {
        lblPercentual.Text = e.Percent.ToString() + "%";
    });
}

If I try to pass the progressbar as param, it doesn't work, and I get delegate errors.

Milo
  • 3,365
  • 9
  • 30
  • 44
  • 2
    "didn't work" and "got errors" is next to useless for us. How didn't it work? What were the errors? – DavidG Sep 13 '19 at 11:28
  • Possible duplicate: [How to use BackgroundWorker in C#](https://stackoverflow.com/a/15153258/1838048) – Oliver Sep 13 '19 at 11:29
  • new Backup(){..} cannot work, you must refer to the control on the form. Pass the reference as an argument. Fwiw, Winforms is relentlessly OOPy, you do prefer this code inside the Form class until you get up to speed. – Hans Passant Sep 13 '19 at 11:38

0 Answers0