Are theme events a “clean” way?

I stumbled upon some code in a professional library and am not sure if this is a clean way to handle stream calls through a stream.

The code below is in the application form. Thread requests are executed from a class that starts a new thread and receives messages:

private void Library_StatusChanged(object sender, AbstractTestCase.StatusChangedEventArgs e)
{
    if (this.InvokeRequired)
    {
        this.lblProgress.Invoke((MethodInvoker)delegate ()
        {
            lblProgress.Text = "Current state: " + e.Step;
            lblProgress.Refresh();
        }
        );

        this.pbProgess.Invoke((MethodInvoker)delegate ()
        {
            pbProgess.Value = e.Percentage;
            pbProgess.Refresh();
        });

        this.lstStatus.Invoke((MethodInvoker)delegate ()
        {
            lstStatus.Items.Add("    " + e.Step);
            lstStatus.Refresh();

        });

        this.Invoke((MethodInvoker)delegate ()
        {
            this.Refresh();
        });
    }
    else
    {
        lblProgress.Text = "Current state:" + e.Step;
        lblProgress.Refresh();

        pbProgess.Value = e.Percentage;
        pbProgess.Refresh();

        lstStatus.Items.Add("    " + e.Step);
        lstStatus.Refresh();

        this.Refresh();
    }

    Application.DoEvents();
}

Is this a "current state"? I think it's a little dirty ?!

+4
source share
2 answers

In the modern level of technology is used await. If this is not possible, at least simplify the code for one call Invoke. No need to reference each control, just call anywhere in the user interface thread.

InvokeRequired , , .

, "Current state: " + e.Step, , , .

Application.DoEvents - . , , , Invoke, ?! .

lstStatus.Refresh(); , , . ( ).

+5

invoke, .

:

private void Library_StatusChanged(object sender, AbstractTestCase.StatusChangedEventArgs e)
{
    this.lblProgress.Invoke((MethodInvoker)delegate ()
    {
        lblProgress.Text = "Current state: " + e.Step;
    });

    this.pbProgess.Invoke((MethodInvoker)delegate ()
    {
        pbProgess.Value = e.Percentage;
    });

    this.lstStatus.Invoke((MethodInvoker)delegate ()
    {
        lstStatus.Items.Add("    " + e.Step);
    });
}
+1

Source: https://habr.com/ru/post/1618451/


All Articles