Question

I have 2 parallel calls ,nCallAsync calls a database SP to do some insert in Table A, vCallAsync calls a web service and logs the data in Table A . After that i read the data from Table A. But in my code i can see nCallAsync executing in profiler before the Table A read , but there is no data from nCallAsync , only way to get data is if i stop the other task all together

Task<int> nCTask = nCallAsync();
Task<int> vCTask = vCallAsync();
int n = await nCTask;
int v = await vCTask;

I am so confused by this behavior

Any help is really appreciated

async Task<int> nCallAsync()
{
    int i = 0;
    i =     Logdata(this.Id, this.var1, var2);
    return i;
}
Was it helpful?

Solution

Your async method has no await in it. You should see a compiler warning you telling you exact that. Because there is no await, there is no asynchrony; you have an entirely synchronous method that just happens to wrap its result in a Task.

There are two general options here.

  1. Change the actual IO that you are doing from synchronous IO to asynchronous IO. The web service code can be modified to generate task returning methods, rather than methods that block while the IO is taking place. You can then await those IO methods, rather than having your async method calling blocking IO.

  2. Just make your entire application synchronous. Get rid of async on nCallAsync (and remove the Async suffix). Then just call the method using Task.Run to perform the operation in another thread. This option isn't nearly as preferable, as you're having a thread pool thread sitting around doing nothing but waiting on an asynchronous operation, but if it's not possible or feasible to actually use entirely asynchronous IO, sometimes you have to resort to this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top