0

I am trying to understand the difference between these lines of code. I have an object of memorystream called arContents and works fine when its size is small. I see the file upload.

blockBlob.UploadFromStreamAsync(arContents)

But when the memorystream is large, the above code runs without errors but uploads no file. However when I added the WAIT() function call, this worked.

blockBlob.UploadFromStreamAsync(arContents).Wait();

I would like to understand first of all What is wait call doing and why is it an async call. Guess want to know the difference between asynch and synch calls too.

Also, I have seen the await also.. code like this

await blockBlob.UploadFromStreamAsync(arContents)

What is the difference?

Thanks

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Sarah
  • 1,199
  • 2
  • 21
  • 42
  • 1
    Possible duplicate of [What's the difference between Task.Start/Wait and Async/Await?](https://stackoverflow.com/questions/9519414/whats-the-difference-between-task-start-wait-and-async-await) – Peter Duniho Aug 19 '19 at 23:21

1 Answers1

1

I see you tagged multithreading on your question, so I think it is important to note that asynchronous and multi-threading are two completely different things. Read through Microsoft's article called The Task asynchronous programming model in C# for a good explanation of how they are different. The example about cooking really helps make this point clear.

When you call an asynchronous method, the task (whatever that is) will start. But you usually also want to either:

  1. Know that it finished successfully, or
  2. Use the returned result from the task (like, if it was retrieving data)

How and when you do that is when things get interesting. The point of asynchronous programming is to allow the thread to go and do something else while waiting for something else to happen (a network request, data from the hard drive, etc) rather than the thread just sitting idle until the task completes.

You have probably experienced some programs that completely lock up and don't let you do anything while it's doing something. That is what asynchronous programming allows you to avoid.

In your first example, you aren't waiting for the result at all. That's often called "fire and forget". You start the task, then code execution immediately moves on to the next line and you will never know if the task completed successfully.

Using .Wait() is not asynchronous at all. It will lock up the thread until the task finishes. Worse than that, when you try to force an asynchronous method to be synchronous, it can sometimes cause a deadlock that your application cannot recover from. Search for c# async wait deadlock and you'll find many examples and explanations.

Ideally, you will want to use await. This is what happens when you use it:

  1. The method (UploadFromStreamAsync) is called.
  2. When an asynchronous operation starts, UploadFromStreamAsync returns a Task.
  3. The await keyword will look at the Task, and if it is not complete, it will put the rest of the current method on the "to do" list for when the Task does finish and return a new Task to the calling method.

As long as you have used async all the way up the call stack, then at that point, the thread can go and do something else it has on its "to do" list. In ASP.NET it could be working on a new request that came in. In a desktop app it could be responding to user input. That would happen on the same thread.

Whenever that task finishes, then await extracts the returned value from the Task. If the method returned just a Task, then that is similar to void (no return value). If it is Task<T>, then it will return the object of type T. Then your code resumes execution after the await line.

That all sounds complicated, but you don't really need to understand completely what it's doing, and that's by design. This feature of C# lets you use asynchronous programming in a way that looks very similar to normal synchronous programming. For example:

public async Task Upload() {
     ...
    await blockBlob.UploadFromStreamAsync(arContents);
}

Will do exactly the same as this:

public void Upload() {
     ...
    blockBlob.UploadFromStream(arContents);
}

They look very similar, except that using async/await will give you the benefits I talked about above and the second will not.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84