Race condition with async/await, how to resolve - c#

I have a problem with async/await in C#, i need it to get some object called Trades, after i get it, it needs to SAVE it. Problem is, with async/await, it is doing the SAVE first, and then go and get my trade objects. How do i ensure i get the objects first, and then does the saving.... here is my code...
private async void OnRefresh()
{
try
{
var trades = await ExchangeServiceInstance.GetTrades("");
mmTrades = new ObservableCollection<EEtrade>(trades);
tradeListView.ItemsSource = mmTrades;
}
catch { }
}
public async void OnSignalReceived()
{
// THIS NEEDS TO FINISH FIRST, BUT IT DOESN'T
await tradeListView.Dispatcher.InvokeAsync((Action)async delegate
{
if (ExchangeServiceInstance.SelectedTabIndex == CURRENT_TAB_INDEX_ITEM)
{
await Task.Delay(MMConfig.DELAYMILLISEC);
OnRefresh();
}
});
// SOMEHOW THIS GETS CALLED FIRST BEFORE THE ABOVE GETS TO FINISH!
await OnSaveTrades();
}
public async Task<int> OnSaveTrades()
{
foreach (var trade in mmTrades)
{
await ExchangeServiceInstance.OnInsertDoneTrade(trade);
}
return mmTrades.Count;
}
Any ideas guys? Thanks!

The problem is your OnRefresh method. Because the return type is void the method is not awaited [Check out this answer]. In addition you dont even try to await for the method inside your delegate
Changing the method to the following:
private async Task OnRefresh()
{
try
{
var trades = await ExchangeServiceInstance.GetTrades("");
mmTrades = new ObservableCollection<EEtrade>(trades);
tradeListView.ItemsSource = mmTrades;
}
catch { }
}
And await this method inside your delegate, should solve your problem:
public async void OnSignalReceived()
{
// THIS NEEDS TO FINISH FIRST, BUT IT DOESN'T
await tradeListView.Dispatcher.InvokeAsync((Action)async delegate
{
if (ExchangeServiceInstance.SelectedTabIndex == CURRENT_TAB_INDEX_ITEM)
{
await Task.Delay(MMConfig.DELAYMILLISEC);
await OnRefresh();
}
});
// SOMEHOW THIS GETS CALLED FIRST BEFORE THE ABOVE GETS TO FINISH!
await OnSaveTrades();
}

The use of (Action)async is basically the same as async void, and async void is almost always a mistake. Specifically, the consumer cannot know the outcome (unless it faults synchronously). The dispatcher here isn't really thinking of async.
If we assume that you must use the dispatcher here, perhaps a workaround might be to use something like a SemaphoreSlim (or maybe a TaskCompletionSource<something>) that you signal at the end of your async work (even in the exception case), and then await that; untested, but:
var tcs = new TaskCompletionSource<bool>();
await tradeListView.Dispatcher.InvokeAsync((Action)async delegate
{
try {
if (ExchangeServiceInstance.SelectedTabIndex == CURRENT_TAB_INDEX_ITEM)
{
await Task.Delay(MMConfig.DELAYMILLISEC);
OnRefresh();
}
tcs.TrySetResult(true);
} catch (Exception ex) {
tcs.TrySetException(ex);
}
});
await tcs.Task; // ensure the async work is complete
await OnSaveTrades();

First of all, you are using the async void pattern a lot. This is really bad practice for a number of reasons. You should stop doing that.
The problem here is that OnRefresh is again an async void method that can't be awaited but should be:
private async Task OnRefresh()
{
try
{
var trades = await ExchangeServiceInstance.GetTrades("");
mmTrades = new ObservableCollection<EEtrade>(trades);
tradeListView.ItemsSource = mmTrades;
}
catch { }
}
In your OnSignalReceived method change the call to OnRefresh(); to await OnRefresh();

Related

Async inside Loop

I try to test this structure but it never works.
I believe I have misunderstood it or lack knowledge in concurrency.
Please light me to the correct direction.
I call a save method but this never saves.
My code :
public async Task<MyObject> GetMyObject_ById(int id)
{
var responseDto = await this.RestApi.QueryMyObject_ById(id).ConfigureAwait(false);
if (responseDto != null)
{
return this.mapper.Map<MyObject>(responseDto);
}
else
{
return null;
}
}
public async Task<MyObject> UpdateMyObject(int id, MyObject updatevalue)
{
var dto = this.mapper.Map<MyObjectDto>(updatevalue);
var updateinfo = await this.RestApi.Update(id, dto).ConfigureAwait(false);
return this.mapper.Map<MyObject>(updateinfo);
}
private void Save()
{
foreach (MyObject pb in this.ListToSave)
{
this.SaveValue(pb.Id, pb.Status));
}
}
private async Task<MyObject> SaveValue(int id,string status)
{
var info = this.GetMyObject_ById(id).Result;
info.Status = status;
return await this.RestApi.UpdateMyObject(id, info).ConfigureAwait(false);
}
Just to summarize the comments. You should replace this.GetMyObject_ById(id).Result;
with await this.GetMyObject_ById(id); to avoid blocking. .Result and .Wait() should be avoided since they can cause deadlocks.
You probably want your save method to look like:
private async void Save()
{
try
{
foreach (MyObject pb in this.ListToSave)
{
await this.SaveValue(pb.Id, pb.Status));
}
}
catch(Exception e){
// handle exception
}
}
or
private async Task Save()
{
foreach (MyObject pb in this.ListToSave)
{
await this.SaveValue(pb.Id, pb.Status));
}
}
When using async methods you should always have some way to handle the result, even if the result might be an exception. This also executes each save call after the previous has completed, this is typically not a bad idea since it helps avoid threading issues.

await operator not stopping continuation of other method calls

I have three methods that look like below
private async Task checkPhotoLibraryAccess()
{
PHPhotoLibrary.RequestAuthorization(status =>
{
switch (status)
{
//stuff here
}
}
}
private async Task checkDeviceAuthorizationStatus()
{
var status = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);
switch (status)
{
//stuff here
}
}
private void displayAppBar()
{
AppBar.IsVisible = true;
}
I would like the execution of the first two methods to complete before calling the third. I have researched the issue and have found ways of doing so using the await operator and Wait() method. However my implementations has not worked. Here is my code below.
Attempt 1
private async void MyMethod()
{
await checkPhotoLibraryAccess();
await checkDeviceAuthorizationStatus();
displayAppBar(); //THIS IS CALLED BEFORE COMPLETION OF TWO ABOVE
}
Attempt 2
private async void MyMethod()
{
checkPhotoLibraryAccess().Wait();
checkDeviceAuthorizationStatus().Wait();
displayAppBar(); //SAME ISSUE
}
Any suggestions on how to get this to work?
To get a third method to be executed based on the completion of two other methods you use the WhenAll method of the Task class.
var t1 = CheckPhotoLibraryAccess();
var t2 = CheckDeviceAuthorization();
await Task.WhenAll(t1, t2).ContinueWith(t => DisplayAppBar());
#Yacoub Massad and #SLaks reminded me that in order for await to work on a method call, the method needs to contain an await operator.
So I changed my PhotoLibraryAccess method to contain an await operator and placed its method call right before the DisplayAppBar call.
private async Task checkPhotoLibraryAccess()
{
var status = await PHPhotoLibrary.RequestAuthorizationAsync();
//stuff here
}
And then ...
private async void MyMethod()
{
checkDeviceAuthorizationStatus(); //Removed async Task stuff
await checkPhotoLibraryAccess();
displayButtons(); //IT WORKS
}

Multiple infinte loops using async and await

I am trying to generate different objects and insert each object into it's respective List using await and async using the code below:
static List<ClassA> classAList = new List<ClassA>();
static List<ClassB> classBList = new List<ClassB>();
public async void GenerateUsers()
{
await GenerateClassA();
await GenerateClassB();
}
private static Task GenerateClassA()
{
while(true)
{
Thread.Sleep(3000);
classAList .Add(new ClassA());
Console.WriteLine(classAList.Count);
}
}
private static Task GenerateClassB()
{
while (true)
{
Thread.Sleep(6000);
classBList .Add(new ClassB());
Console.WriteLine(classBList.Count)
}
}
When I call GenerateUsers like
UserContainer uc = new UserContainer(); //Class GenerateUsers is defined
uc.GenerateUsers();
Console.WriteLine("Generating.....");
The program does not exit GenerateClassA and Generating..... is not printed to the screen.
How can I generate different objects in an infinite loop using await and async for each infinite loop.
You are using await GenerateClassA();, but GenerateClassA doesn't do anything continuation related - it simply run to completion promising to return a Task. Except: it can't run to completion, because while(true). So yes, this won't work.
Something doesn't become async just beacause you add async. That enables genuinely async operations to be coordinated: nothing more, nothing less.
The methods you are calling are not async, and so they execute synchronously. Indeed, only the first method, GenerateClassA(), is ever even called. That method never returns, so you never get as far as calling the second. Probably you wanted something more like this:
public void GenerateUsers()
{
GenerateClassA();
GenerateClassB();
}
private static async void GenerateClassA()
{
while(true)
{
await Task.Delay(3000);
classAList.Add(new ClassA());
Console.WriteLine(classAList.Count);
}
}
private static async void GenerateClassB()
{
while (true)
{
await Task.Delay(6000);
classBList.Add(new ClassB());
Console.WriteLine(classBList.Count)
}
}
Note that the void return types prevent you from observing the tasks, such as exceptions that might occur. It's not advised. But it's consistent with the code you originally posted.

Async Programming without Await?

I'm working on a series of methods that execute many different database calls using entity framework. Many of these methods can run asynchronously as I really don't care about their output, and don't rely on them.
However, when I try implementing certain methods, I get a warning from the compiler saying: "Because this call is not awaited, the current method continues to run before the call is completed"
But to me, this seems like that's my desired behavior, as I don't care what the methods do.
Here's an example of the methods
public async Task SetupAccessControl(int objectTypeId, int objectId, int? organizationId)
{
using (var context = new SupportContext(CustomerId))
{
... // omitted for brevity
if (objectTypeId == (int) ObjectType.User)
{
AddToUserRoleBridge("Everyone", objectId);//Warning on this line
AddToUserRoleBridge("Default", objectId); //Warning on this line
}
... // omitted for brevity
}
}
public async Task AddToUserRoleBridge(string role, int userId)
{
using (var context = new SupportContext(CustomerId))
{
var defaultRole = context.Roles.FirstOrDefault(n => n.Name == role);
if (defaultRole != null)
{
var urb = new UserRoleBridge
{
RoleId = defaultRole.Id,
UserId = userId
};
context.UserRoleBridges.Add(urb);
await context.SaveChangesAsync();
}
}
}
edit
Essentially, when I run the main function, I want a series of method calls to all fire off somewhat simultaneously and handle everything in their own threads so that I don't have to worry about it. Here is a pseudo-code example.
public async void RunAllAsync() {
taskA(*some value*);
taskA(*some value*);
taskB(*some value*);
taskB(*some value*);
await AllTasksCompleted
}
public async Task taskA(int item){
//do something with item
}
public async Task taskB(int item) {
subTaskB(*some value*)
subTaskB(*some value*)
}
public async Task subTaskB(int item) {
// do something
}
In the above example, when #RunAllAsync is called, every function call it makes (and the function calls they make) are fired off simultaneously. When all of these calls are completed, whatever method called #RunAllAsync would continue to execute.
If you're not using await, the async keyword doesn't really do anything useful and you can leave it off. You can await a method returning a Task regardless of whether it's marked as async or not.
public Task DoSomethingAsync()
{
Task someTaskJustLikeANormalReturnValue = Task.Delay(1000);
return someTaskJustLikeANormalReturnValue;
}
// later...
public async Task SomeOtherFunction()
{
// You can await DoSomethingAsync just like any async method, because
// you're really awaiting the Task which got returned.
await DoSomethingAsync();
}
In your case I would probably collect the tasks and await them all together:
public async Task SetupAccessControl(int objectTypeId, int objectId, int? organizationId)
{
var tasks = new List<Task>();
using (var context = new SupportContext(CustomerId))
{
... // omitted for brevity
if (objectTypeId == (int) ObjectType.User)
{
tasks.Add(AddToUserRoleBridge("Everyone", objectId));
tasks.Add(AddToUserRoleBridge("Default", objectId));
}
... // omitted for brevity
}
await Task.WhenAll(tasks.ToArray());
}
This allows you to pass the decision up to the caller of whether to await on the subtasks or not. This will also allow the any caller to unwrap any exceptions if they happen in AddToUserRoleBridge.
You can do it two ways:
Change the signature of AddToUserRoleBridge to return void will
remove the warning (but nothing can await it then).
Store the result of the call. var ignoreme=AddToUserRoleBridge(...) will remove the warning as well.

await not blocking until Task finishes

I am trying to block RequestHandler.ParseAll() with await ConsumerTask;, but when i set a breakpoint there, i ALWAYS get the "Done..." output first... and then Parse2() fails with a NullReferenceException. (thats my guess: "the GC starts cleaning up because _handler got out of scope")
Anyway, I can't figure out why that happens.
class MainClass
{
public async void DoWork()
{
RequestHandler _handler = new RequestHandler();
string[] mUrls;
/* fill mUrls here with values */
await Task.Run(() => _handler.ParseSpecific(mUrls));
Console.WriteLine("Done...");
}
}
static class Parser
{
public static async Task<IEnumerable<string>> QueryWebPage(string url) { /*Query the url*/ }
public static async Task Parse1(Query query)
{
Parallel.ForEach(/*Process data here*/);
}
public static async Task Parse2(Query query)
{
foreach(string line in query.WebPage)
/* Here i get a NullReference exception because query.WebPage == null */
}
}
sealed class RequestHandler
{
private BlockingCollection<Query> Queue;
private Task ConsumerTask = Task.Run(() => /* call consume() for each elem in the queue*/);
private async void Consume(Query obj)
{
await (obj.BoolField ? Parser.Parse1(obj) : Parser.Parse2(obj));
}
public async void ParseSpecific(string[] urls)
{
foreach(string v in urls)
Queue.Add(new Query(await QueryWebPage(v), BoolField: false));
Queue.CompleteAdding();
await ConsumerTask;
await ParseAll(true);
}
private async Task ParseAll(bool onlySome)
{
ReInit();
Parallel.ForEach(mCollection, v => Queue.Add(new Query(url, BoolField:false)));
Queue.CompleteAdding();
await ConsumerTask;
/* Process stuff further */
}
}
struct Query
{
public readonly string[] WebPage;
public readonly bool BoolField;
public Query(uint e, IEnumerable<string> page, bool b) : this()
{
Webpage = page.ToArray();
BoolField = b;
}
}
CodesInChaos has spotted the problem in comments. It stems from having async methods returning void, which you should almost never do - it means you've got no way to track them.
Instead, if your async methods don't have any actual value to return, you should just make them return Task.
What's happening is that ParseSpecific is only running synchronously until the first await QueryWebPage(v) that doesn't complete immediately. It's then returning... so the task started here:
await Task.Run(() => _handler.ParseSpecific(mUrls));
... completes immediately, and "Done" gets printed.
Once you've made all your async methods return Task, you can await them. You also won't need Task.Run at all. So you'd have:
public async void DoWork()
{
RequestHandler _handler = new RequestHandler();
string[] mUrls;
await _handler.ParseSpecific(mUrls);
Console.WriteLine("Done...");
}
...
public async TaskParseSpecific(string[] urls)
{
foreach(string v in urls)
{
// Refactored for readability, although I'm not sure it really
// makes sense now that it's clearer! Are you sure this is what
// you want?
var page = await QueryWebPage(v);
Queue.Add(new Query(page, false);
}
Queue.CompleteAdding();
await ConsumerTask;
await ParseAll(true);
}
Your Reinit method also needs changing, as currently the ConsumerTask will basically complete almost immediately, as Consume will return immediately as it's another async method returning void.
To be honest, what you've got looks very complex, without a proper understanding of async/await. I would read up more on async/await and then probably start from scratch. I strongly suspect you can make this much, much simpler. You might also want to read up on TPL Dataflow which is designed to make producer/consumer scenarios simpler.

Categories