Refactor to remove Taks.Run from my code - c#

I just wrote the following code
public void Save()
{
while (this.IsAsyncInProcess)
Thread.Sleep(100);
this.customer.OrderCount = this.orders.Count();
this.customer.OrderTotal = this.orders.Sum(o => x.Total);
this.customerRepo.Save();
}
public async Task LoadAsync()
{
this.IsAsyncInProcess = true;
this.customer = await this.customerRepo.GetCustomerAsync(...);
this.orders = await this.customerRepo.GetOrdersAsync(...);
this.IsAsyncInProcess = false;
}
Now I had a classical deadlock because, after this.orders completed, it would wait for the gui thread to resume in order to set this.IsAsyncInProcess to false. However, the gui thread was busy inside Save()
Now I refactored LoadAsync to
public async Task LoadAsync()
{
await Task.Run(async () =>
{
this.IsAsyncInProcess = true;
this.customer = await this.customerRepo.GetCustomerAsync(...);
this.orders = await this.customerRepo.GetOrdersAsync(...);
this.IsAsyncInProcess = false;
});
}
I can't just refactor Save to SaveAsync for compability reasons.
Is there a better way to achive this, without using Task.Run?

You can use async locking. If you can't change the signiture of Save then you can just proxy to an async local function.
static SemaphoreSlim sem = new SemaphoreSlim(1,1);
public void Save()
{
SaveAsync();
public async Task SaveAsync()
{
await sem.WaitAsync();
try{
this.customer.OrderCount = this.orders.Count();
this.customer.OrderTotal = this.orders.Sum(o => x.Total);
this.customerRepo.Save();
}finally{
sem.Release();
}
}
}
public async Task LoadAsync()
{
await sem.WaitAsync();
try{
this.customer = await this.customerRepo.GetCustomerAsync(...);
this.orders = await this.customerRepo.GetOrdersAsync(...);
}finally{
sem.Release();
}
}
or even better create your own async lock
static SemaphoreSlim sem = new SemaphoreSlim(1,1);
public static async Task<IDisposable> LockAsync(){
await sem.WaitAsync();
return Disposable.Create(()=>sem.Release());
}
public void Save()
{
SaveAsync();
public async Task SaveAsync()
{
using(await LockAsync()){
this.customer.OrderCount = this.orders.Count();
this.customer.OrderTotal = this.orders.Sum(o => x.Total);
this.customerRepo.Save();
}
}
}
public async Task LoadAsync()
{
using(await LockAsync()){
this.customer = await this.customerRepo.GetCustomerAsync(...);
this.orders = await this.customerRepo.GetOrdersAsync(...);
}
}

Related

C# Issues returning the proper Task<bool> value from and async Task<bool> function [duplicate]

I have such a function:
public async Task<bool> DoSomething()
{
var tcs = new TaskCompletionSource<bool>();
// Here is the problem. I need to keep this line because I wait on something asynchronously, but the function must return bool and I can't just return tcs.Task
while(something)
await Task.Delay(100);
someobject.somevent += () => {
// do some sht
tcs.SetResult(true);
}
// it doesn't work
return tcs.Task;
}
It's just a fake code but I have real situation where I need this. I want to keep DoSomething asynchronous but I also want to keep Task.Delay/Sleep in it. How do I do this in not-async function returning just Task?
UPDATE:
THIS WORKS:
class Program
{
static TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
static Task<bool> Test()
{
// tcs = new TaskCompletionSource<bool>();
Task.Factory.StartNew(() =>
{
Console.WriteLine("Waiting...");
Thread.Sleep(5000);
Console.WriteLine("Setting result");
if(tcs.TrySetResult(true))
Console.WriteLine("Result has been set");
});
return tcs.Task;
}
static async Task Test2()
{
Console.WriteLine("Starting awaiting");
var result = await Test();
Console.WriteLine(result.ToString());
}
static void Main(string[] args)
{
Test2();
Console.ReadKey(false);
}
}
and this doesn't
static async Task<bool> Test()
{
// tcs = new TaskCompletionSource<bool>();
Task.Factory.StartNew(() =>
{
Console.WriteLine("Waiting...");
Thread.Sleep(5000);
Console.WriteLine("Setting result");
if(tcs.TrySetResult(true))
Console.WriteLine("Result has been set");
});
return await tcs.Task;
}
what's worse, I have tested it in my windows forms app and awaiting tcs.Task caused weird crash coming from System.Threading....dll
If I understand correctly (it's tricky because your question isn't that easy to follow) you can restate things as follows:
public async Task<bool> DoSomething()
{
var tcs = new TaskCompletionSource<bool>();
someobject.somevent += () => {
// do some sht
tcs.SetResult(true);
}
return await tcs.Task;
}
The whole thing will come out a lot more elegantly if you separate out the logic of turning the event firing into a Task into its own method.
public static Task<bool> WhenSomeEvent(this SomeObject someobject)
{
var tcs = new TaskCompletionSource<bool>();
Action handler = null;
handler = () =>
{
tcs.SetResult(true);
someobject.SomeEvent -= handler;
};
someobject.SomeEvent += handler;
return tcs.Task;
}
This allows you to write the business logic separately, without mixing in all of the logic of translating the event into a Task:
public async Task<bool> DoSomething()
{
while(something)
await Task.Delay(100);
return await someobject.WhenSomeEvent();
}

Task caused UI Freezing - WPF

The structure of my code is shown below:
internal async Task ButtonClickMethod()
{
await this.InitializeMethod();
}
internal async Task InitializeMethod()
{
await this.Call();
}
private async Task Call()
{
await this.CallTasks();
}
private async Task CallTasks()
{
List<Task> tasks = new List<Task>();
tasks.Add(/*time-consuming task A*/);
tasks.Add(/*time-consuming task B*/);
tasks.Add(/*time-consuming task C*/);
tasks.Add(/*time-consuming task D*/);
await Task.WhenAll(tasks);
}
Task defined as follows:
/*time-consuming task*/
internal async Task TimeConsuming()
{
// code...
await Task.Run(() =>
{
// code... A/B/C/D
});
// code...
}
Is there any problem with this code that will cause the UI to freeze?
Is the problem caused by Task.WhenAll?
Use this instead :
private async void ClickButton(object sender, RoutedEventArgs e)
{
await ThatFirstMethod(any parameters);
}
private async Task ThatFirstMethod(any parameters)
{
await Task.Run(() => {
ThatFirstTimeConsumingMethod(any param);
});
await Task.Run(() => {
ThatSecondTimeConsumingMethod(any param);
});
}
private bool ThatFirstTimeConsumingMethod(any param)
{
//code
return true;
}
.....
If you have to access UI Elements you will not be able to do so, beacause your "heavy" methods are being processed by another Thread.
Here's a solution :
private bool ThatFirstTimeConsumingMethod(any param)
{
string s = "";
Application.Current.Dispatcher.Invoke(() => {
s = myTextBox.Text;
});
// do what you want with the s variable
Application.Current.Dispatcher.Invoke(() => {
myTextBox.Text = s;
});
return true;
}
So basically, you're asking your main Thread to execute an action (where your UI is getting processed) with Application.Current.Dispatcher.Invoke. And when you're in the await Task.Run you're asking another thread to process the heavy action.

How to wait on task that runs async method

I'm writing WPF app and recently started working with await/async so the GUI thread does not perform any time consuming operations.
My problem is I want to load two collections from db asynchronously using Entity framework. I know I can't call two ToListAsync() methods on DbContext so I wanted to use tasks.
I wrote async method LoadData() that should wait on completing the LoadNotifications() and then call LoadCustomers().
But when the execution gets to await this.context.MailingDeliveryNotifications.ToListAsync(); it creates another task and somehow it doesn't care about the task.Wait() in my LoadData() method, so it calls LoadCustomers() before completing the first call on DbContext.
The code:
public async void LoadData()
{
Task task = this.LoadNotifications();
task.Wait();
await this.LoadCustomers();
}
private Task LoadNotifications()
{
return Task.Run(() => this.LoadNotificationsAsync());
}
private async void LoadNotificationsAsync()
{
List<MailingDeliveryNotification> res = await this.context.MailingDeliveryNotifications.ToListAsync();
this.Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
}
private Task LoadCustomers()
{
return Task.Run(() => this.LoadNotificationsAsync());
}
private async void LoadCustomersAsync()
{
List<Customer> res = await this.context.Customers.ToListAsync();
this.Customers = new ObservableCollection<Customer>(res);
}
I know I can solve this using this code
public async void LoadData()
{
List<MailingDeliveryNotification> res = await this.context.MailingDeliveryNotifications.ToListAsync();
this.Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
List<Customer> res2 = await this.context.Customers.ToListAsync();
this.Customers = new ObservableCollection<Customer>(res2);
}
but when I will need to add another collection to load from db, this method will grow to much. I want to keep my code Clean.
Simplify your code:
public async Task LoadDataAsync()
{
await LoadNotificationsAsync();
await LoadCustomersAsync();
}
private async Task LoadNotificationsAsync()
{
var res = await context.MailingDeliveryNotifications.ToListAsync();
Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
}
private async Task LoadCustomersAsync()
{
var res = await context.Customers.ToListAsync();
Customers = new ObservableCollection<Customer>(res);
}
Or probably just:
public async Task LoadDataAsync()
{
Notifications = new ObservableCollection<MailingDeliveryNotification>(
await context.MailingDeliveryNotifications.ToListAsync());
Customers = new ObservableCollection<Customer>(
await context.Customers.ToListAsync());
}

Cancel and reactivate async task

I want to create a statusbar. The status should be set from any method inside the class. If the status is set it should be for 5000ms visible. After 5000ms the status should be empty. Sometimes it can happen, that I want to set a status when an old status is still active. For this case the old status should be overwritten and the await Task.Delay(5000); should be reset and start counting from 0.
My current code looks like this:
public CancellationTokenSource tokenSource { get; set; }
public CancellationToken token { get; set; }
public async Task SetStatusMessage(string pStatusMessage)
{
tokenSource.Cancel();
await Task.Run(async () =>
{
if (token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
}
this.Dispatcher.Invoke(() =>
{
this.txtStatusMessage.Text = pStatusMessage;
});
await Task.Delay(5000, token);
this.Dispatcher.Invoke(() =>
{
this.txtStatusMessage.Text = "";
});
}, token);
public async void AnyMethod()
{
await this.SetStatusMessage("Hello World");
}
This isn't working, because I cancel the task before it is running. That's why I get an OperationCanceledException (?).
This is what I would do. I have no idea why you use Task.Run() when you are using async.
public async Task SetStatusMessage(string pStatusMessage)
{
CancellationTokenSource localToken;
try
{
if (tokenSource != null)
tokenSource.Cancel();
tokenSource = new CancellationTokenSource();
localToken = this.tokenSource;
this.txtStatusMessage.Text = pStatusMessage;
await Task.Delay(5000, localToken.token);
this.txtStatusMessage.Text = "";
}
catch (TaskCanceledException) {}
finally
{
localToken.Dispose();
}
}

C# - How to run multi await in one method?

I don't know how to run multi await methods in single method. For example my code as below:
public static async Task<bool> Authenticate()
{
bool authen = false;
string message = String.Empty;
try
{
session = await FacebookSessionClient.LoginAsync("user_about_me,read_stream");
fbAccessToken = session.AccessToken;
fbFacbookID = session.FacebookId;
await saveProfile(fbFacebookID); //error here,my app is closed at here
authen = true;
}
catch (InvalidOperationException e)
{
authen = false;
}
return authen;
}
And I have method save profile
public async static void saveProfile(string fbFacbookID)
{
string response = string.Empty;
if (!string.IsNullOrEmpty(fbFacbookID))
{
response=await StaticClass.getJsonStream(string.Format("http://graph.facebook.com/{0}", fbFacbookID));
JObject _object = JObject.Parse(response);
SaveValueSetting("usernameFB",(string)_object["username"]);
}
else
{
return;
}
}
But I cannot run method? So how do I fix it?
you can use below mentioned code.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MyMethod();
MyMethod1();
}
public async Task MyMethod()
{
Task<int> longRunningTask = LongRunningOperation();
//indeed you can do independent to the int result work here
//and now we call await on the task
int result = await longRunningTask;
//use the result
MessageBox.Show(result.ToString());
}
public async Task MyMethod1()
{
Task<int> longRunningTask = SecondMethod();
//indeed you can do independent to the int result work here
//and now we call await on the task
int result = await longRunningTask;
//use the result
MessageBox.Show(result.ToString());
}
public async Task<int> LongRunningOperation() // assume we return an int from this long running operation
{
await Task.Delay(5000); //5 seconds delay
return 1;
}
public async Task<int> SecondMethod()
{
await Task.Delay(2000);
return 1;
}

Categories