1

I am trying to use Aync method. It's working in a way. But it's not working in some other context.

Working example

dropBox.DownloadFileAsync(csvEntry.Path)
                    .ContinueWith(task =>
                    {
                        // Save file to "C:\Spring Social.txt"
                        using (FileStream fileStream = new FileStream(tempCsvPath, FileMode.Create))
                        {
                            fileStream.Write(task.Result.Content, 0, task.Result.Content.Length);
                        }
                    });

Instead of saving the file, I am trying to return the byte array in the following way. But it's not working. It is returning null.

 byte[] returnArray = null;
                dropbox.DownloadFileAsync(filePath)
                        .ContinueWith(task =>
                        {
                            returnArray = new byte[task.Result.Content.Length];
                            task.Result.Content.CopyTo(returnArray, 0);
                        });
                return returnArray;

Can somebody correct me?

Thanks

1
  • 3
    Surely DownloadFileAsync is asynchronous, and so therefore you should not expect it to have completed by the time you hit the next statement (your "return" statement)? Commented Dec 12, 2012 at 23:56

1 Answer 1

4

In this code:

            byte[] returnArray = null;
            dropbox.DownloadFileAsync(filePath)
                    .ContinueWith(task =>
                    {
                        returnArray = new byte[task.Result.Content.Length];
                        task.Result.Content.CopyTo(returnArray, 0);
                    });
            return returnArray;

DownloadFileAsync() is executed on a thread. When the ContinueWith() is executed your function that calls DowloadFileAsync() already has returned.

You would need to do something like this:

     Task<T> Download(string filePath)
     {             
            return dropbox.DownloadFileAsync(filePath)
                    .ContinueWith(task =>
                    {
                        returnArray = new byte[task.Result.Content.Length];
                        task.Result.Content.CopyTo(returnArray, 0);

                        return returnArray;
                    });
      }

Call it like this:

     var task = Download("myfile");

     task.ContinueWith(t => 
      { 
           var returnArray = t.Result;
           ...

      }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply. I have changed my code as you mentioned and I am relying on task.Result where task is returned by Download method. Is it the right way of doing it?
It's not “executed on a background thread”. A big point of using async is that it doesn't block any thread when doing IO.
it's still a thread where the async code is executed, or not?
It's not “executed on a background thread -> it IS executed in the background, but when you say something = t.result; you BLOCK the current thread until the task is done. If you dont want that, use "await" (if you use VS2012 and target FW 4.x)
@igrimpe but the something = t.result is only executed in the continuation, so the thread (UI thread in this case I guess), where Download() is called, is not blocked, or not?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.