I'm currently writing an component where I already have a synchronous method to download files from a server. Here's the signature:
public Stream DownloadFile(IData data)
Now I wanted to have an async version of this method and tried this:
public async Task<Stream> DownloadFileAsync(IData data)
{
Stream stream = null;
await Task.Run(() =>
{
DownloadFile(data);
});
return stream;
}
But resharper is showing me a squiggle line under return stream;
"expression is always null". Can somebody tell me what is wrong with my code?
What I want to do is run the DownloadFile method in background and return controlflow to the caller.
EDIT: here is the DownloadFile method:
var ms = new MemoryStream();
using (var dtService = new DataTransferService(mConnectionInfo))
{
int bytesRead;
do
{
var buffer = new byte[data.ChunkSize];
bytesRead = dtService.Read(data, buffer);
if (bytesRead == 0)
{
break;
}
data.Offset += bytesRead;
ms.Write(buffer, 0, buffer.Length);
} while (bytesRead > 0);
}
return ms;
Thanks