forum

Home / DeveloperSection / Forums / Task variable ContinueWith, await later?

Task variable ContinueWith, await later?

Anonymous User 2184 19-Aug-2014
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 in an async method. I wonder if the following altered code is still correct with regards to the var task, ContinueWith and await.?

        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 warnings at the ContinueWith statements.


c# c# 
Updated on 19-Aug-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By