Skip to content

Commit

Permalink
Add an example for AsyncFixer04:AsyncCallInsideUsingBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
semihokur authored Jan 20, 2021
1 parent b7a4faa commit 8edf0da
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ AsyncFixer automatically converts `void` to `Task`.

### AsyncFixer04: Fire-and-forget async call inside an *using* block

Inside a `using` block, developers insert a fire-and-forget async call which uses a disposable object as a parameter or target object. It can cause potential exceptions or wrong results. For instance, developers create a `Stream` in the `using` statement, pass it to the asynchronous method, and then `Stream` will be implicitly disposed via a `using` block. When the asynchronous method comes around to writing to `Stream`, it is (very likely) already disposed and you will have an exception.
Inside a `using` block, developers insert a fire-and-forget async call which uses a disposable object as a parameter or target object. It can cause potential exceptions or wrong results. Here is an example:

```
static void foo()
{
var newStream = new FileStream("file.txt", FileMode.Create);
using (var stream = new FileStream("newfile.txt", FileMode.Open))
{
newStream.CopyToAsync(stream);
}
}
```
We copy the contents of the file to another file above. If the file size is big enough to make `CopyToAsync` take non-trivial duration, we will have `ObjectDisposedException` because `Stream` will be implicitly disposed due to the `using` block before `CopyToAsync` is finished. To fix the issue, we need to await asynchronous operations involving disposable objects inside `using` blocks:
```
await newStream.CopyToAsync(stream);
```

### AsyncFixer05: Downcasting from a nested task to an outer task.

Expand Down

0 comments on commit 8edf0da

Please sign in to comment.