XAML with parameter - Backgroundworker

Asked

Viewed 52 times

3

I’m doing a small XAML application that asks for 2 dates. Dates are passed as a parameter to some methods invoked by the Event click button. The problem is that during the execution of the methods the form remains locked.

Using Backgroundworker or another Threads feature to solve this problem?

inserir a descrição da imagem aqui

private void Button_Click(object sender, RoutedEventArgs e)
{
    DateTime dataInicial = new DateTime();
    DateTime dataFinal = new DateTime();
    dataInicial = (DateTime)dtInicial.SelectedDate;
    dataFinal = (DateTime)dtFinal.SelectedDate;

    progressbar1.Visibility = Visibility.Visible;
    progressbar1.IsIndeterminate = true;

    thread = new Thread(() => {         

        relatorio1 r1 = new relatorio1();
        r1.gerarDados(dataInicial, dataFinal);
        relatorio2 r2 = new relatorio2();
        r2.gerarDados(dataInicial, dataFinal);
        relatorio3 r3 = new relatorio3();
        r3.gerarDados(dataInicial, dataFinal);
        relatorio4 r4 = new relatorio4();
        r4.gerarDados(dataInicial, dataFinal);
        MessageBox.Show("Sucesso !!!");          

    });

    thread.Start();
    progressbar1.IsIndeterminate = false;




}

In the current code I solved the locking problem by instantiating a Thread directly through Lambda. The problem is that I can’t change the Form Progressibar properties once the Thread Ends. I would like to set the Isindeterminate event to false but I did not succeed. I believe that with Backgroundworker it is possible.

1 answer

1


The solution to this problem is much simpler than that. Even, one of the biggest advantages of C# is how easy you can work with asynchronous methods without having to manage threads manually.

All you need to do is start the method where all the processing takes place in one Task and wait for her to end up using await. Look at the new version of your code:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var beginDate = begin.SelectedDate;
    var endDate = end.SelectedDate;

    progressBar.Visibility = Visibility.Visible;
    progressBar.IsIndeterminate = true;

    await Task.Run(() =>
    {
        relatorio1 r1 = new relatorio1();
        r1.gerarDados(beginDate , endDate);
        relatorio2 r2 = new relatorio2();
        r2.gerarDados(beginDate , endDate);
        relatorio3 r3 = new relatorio3();
        r3.gerarDados(beginDate , endDate);
        relatorio4 r4 = new relatorio4();
        r4.gerarDados(beginDate , endDate);

    });

    MessageBox.Show("Sucesso!");
    progressBar.Visibility = Visibility.Collapsed;
}

Only one addendum: in the current version of the code, it is possible to click the button several times while the processing takes place, which would cause the reports to be generated several times. To solve this you can use both a lock how to simply disable the button while processing is not completed.

  • Thanks Roney, funfou ! Thanks for the button tip, I chose to disable it during processing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.