I want to retrieve a list of objects from database asynchronously, which of the following is advisable?
All works
Application.StartTask V1
private async void GetObjects(){
var data= await Application.StartTask(()=>DataLayer.GetObjects());
dataGridView1.DataSource= data;
}
Application.StartTask V2
private async void GetObjects(){
await Application.StartTask(()=>{
var data= DataLayer.GetObjects());
Application.Update(this,()=>dataGridView1.DataSource= data);
});
Task.Run
private async void GetObjects(){
var data= await Task.Run(()=>DataLayer.GetObjects());
dataGridView1.DataSource= data;
}
Task.Run and Application.StartTask are the same with the difference that Application.StartTask preserves the session context.
Otherwise you’d have to save it using Application.Current and Application.RunInContext.
Application.Update is necessary to push the UI updates to the browser. When running a task there is no request/response.
You can also change any event (i.e. Click) into an async event which will run asynchronously in a task.
Please login first to submit.