How to show "wait, loading data" using asynchronous programming?

2

How can I use async programming with Async and Await so that it appears in the middle of the "Wait for data loading" grid while searching for data in the database and populating with the DataTable in the Grid?

Programming in Windows Form and C #.

    
asked by anonymous 30.04.2015 / 22:20

1 answer

2

p>

MostraAguarde(true);
var task = Task.Factory.StartNew(() =>
{
    LerDados();
});
task.ContinueWith(
t =>
{
    MostraAguarde(false);
},TaskScheduler.FromCurrentSynchronizationContext());

Create the MostraAguarde(bool mostrar) method to display the message when the passed parameter is true and exit the message when it is false . Create the LerDados() method where the data will be read.

I think the code is easy to understand:

The message is displayed. A Task is created to execute the method that reads the data. When the LerDados() method returns, the Task is terminated and the message is removed.

EDIT
Using await would be something like this:

MostraAguarde(true);
try
{
    await LerDados();
}
catch (Exception e)
{
    ProcessErrors(e);            
}
finally
{
    MostraAguarde(false);
}

The LerDados() method must return Task or Task<T> :

private Task LerDados()
{
    ....
}
    
30.04.2015 / 23:17