---
title: "Task variable ContinueWith, await later?"  
description: "Task variable ContinueWith, await later?"  
author: "Anonymous User"  
published: 2014-08-19  
updated: 2014-08-19  
canonical: https://www.mindstick.com/forum/2054/task-variable-continuewith-await-later  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Task variable ContinueWith, await later?

```
private Task<IUploadProgress>
UploadFileAsync(DriveService service)    {        var title =UploadFileName;        if
(title.LastIndexOf('\\') != -1)        {            title =title.Substring(title.LastIndexOf('\\') + 1);        }        var uploadStream = new System.IO.FileStream(UploadFileName,System.IO.FileMode.Open,           
System.IO.FileAccess.Read);        var insert =service.Files.Insert(new File { Title = title }, uploadStream, ContentType);       
insert.ChunkSize = FilesResource.InsertMediaUpload.MinimumChunkSize * 2;       
insert.ProgressChanged += Upload_ProgressChanged;       
insert.ResponseReceived += Upload_ResponseReceived;        var task = insert.UploadAsync();       
task.ContinueWith(t =>        {            //
NotOnRanToCompletion - this code will be called if the upload fails           
Console.WriteLine("Upload Filed. " + t.Exception);        },
TaskContinuationOptions.NotOnRanToCompletion);       
task.ContinueWith(t =>        {           
Logger.Debug("Closing the stream");           
uploadStream.Dispose();           
Logger.Debug("The stream was closed");        });        return task;    }
```

I'm using part of the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) in an [async](https://www.mindstick.com/forum/23036/calling-an-activity-class-from-async-class) method. I [wonder](https://yourviews.mindstick.com/story/2441/wheatgrass-juice-wonder-for-your-health) if the following altered code is still correct with regards to the [var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) [task](https://www.mindstick.com/forum/161761/what-is-the-difference-between-task-and-thread), ContinueWith and [await](https://www.mindstick.com/blog/304336/asynchronous-programming-with-async-await-task-in-c-sharp).?

```
        var task = insert.UploadAsync();        task.ContinueWith(t =>        {            // NotOnRanToCompletion - this code will be called if the upload fails            Console.WriteLine("Upload Filed. " + t.Exception);        }, TaskContinuationOptions.NotOnRanToCompletion);        task.ContinueWith(t =>        {            Logger.Debug("Closing the stream");            uploadStream.Dispose();            Logger.Debug("The stream was closed");        });        await task;        if (task.Result.Status == UploadStatus.Failed)        {
```

I get [compilation](https://www.mindstick.com/interview/358/which-is-the-first-level-of-compilation-in-the-dot-net-languages) warnings at the ContinueWith statements.

## Replies

### Reply by Pravesh Singh

Hi Ankit, \
try this:

```
try {     var result = await
insert.UploadAsync(); } catch(Exception ex) {   
Console.WriteLine("Upload Filed. " + ex.Message); } finally {    
Logger.Debug("Closing the stream");    
uploadStream.Dispose();    
Logger.Debug("The stream was closed"); }
```

\


---

Original Source: https://www.mindstick.com/forum/2054/task-variable-continuewith-await-later

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
