Async await concept as per my experience
Hi,
Private async void mainMethod()
{
bla;
bool IsInserted = await SaveToDb();
//here we are offloading responsibility from UI thread to separate task. so UI is free until SaveToDb(); done its Job.
yourButtonName.Dispatcher.Invoke(()=> yourButtonName.content("done.."););
}
I am a .Net developer, worked on several desktop wpf application.
In c# 5.0 MS provide Async await feature that simply help us with freedom UI to stuff when some big method being executed.
Suppose I have big method with long operation like DB insertion along with other stuff. I click on some button to trigger that method. at that time WPF freeze the UI because WPF working on other important operation to execute that method. and its unable to update your UI because its doing other stuff.
so here Async await comes in picture.
So in my code I will make my Big Method Async which will return some value with Task type. Like
Private async void mainMethod()
{
//here you can run methods which dont need anything from below method.
//for e.g. update some text on button
yourButtonName.Dispatcher.Invoke(()=> yourButtonName.content("processing.."););
await Task.Run(()=> SaveToDb());
bla;bla;
bool IsInserted = await SaveToDb();
//here we are offloading responsibility from UI thread to separate task. so UI is free until SaveToDb(); done its Job.
yourButtonName.Dispatcher.Invoke(()=> yourButtonName.content("done.."););
}
public async Task<bool> SaveToDb()
{
//this is my lengthy operation code
blaaaaaaaa
blaaaaaaaa
blaaaaaaaa
MyDbClass dbObj = new MyDbClass ();
bool inserted = await Task.Run(() => dbObj.InsertInDB());
MyDbClass dbObj = new MyDbClass ();
bool inserted = await Task.Run(() => dbObj.InsertInDB());
return inserted ;
}
It will not block UI.
JUST KEEP IN MIND WE CAN await Task.Run(() => dbObj.InsertInDB()); because its a background task and UI not needed here.
and now suppose you have method1() and method2() both require UI screen (same or different) then you cant do like below
private async void DoSomething()
{
Label1.Content = "Started....";
await Task.Run(()=> method1());
Label1.Content = "1st one done....";
await Task.Run(()=> method2());
Label1.Content = "2nd done....";
bla bla
Label1.Content = "all done....";
}
method1(): need to select value from UI screen list box
method2(): need to select value from UI screen check box (just Suppose)😼
JUST KEEP IN MIND WE CAN await Task.Run(() => dbObj.InsertInDB()); because its a background task and UI not needed here.
and now suppose you have method1() and method2() both require UI screen (same or different) then you cant do like below
private async void DoSomething()
{
Label1.Content = "Started....";
await Task.Run(()=> method1());
Label1.Content = "1st one done....";
await Task.Run(()=> method2());
Label1.Content = "2nd done....";
bla bla
Label1.Content = "all done....";
}
method1(): need to select value from UI screen list box
method2(): need to select value from UI screen check box (just Suppose)😼
WHY
Because you are using Task.Run for both. they start executing because this async method and immidiatly run the both and then later they will fight for UI 😸 and it will give STA thread Error.
so avoid Task.Run when your both method need UI. instead you can use
Comments
Post a Comment