Reputation: 15039
I have the following code that uses System.Threading.Tasks
private async void UploadDocument(System.IO.FileInfo fileInfo)
{
var someTask = await Task.Run<bool>(() =>
{
// open input stream
using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
{
uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;
// start service client
FileTransferWCF.FileTransferServiceClient client = new FileTransferWCF.FileTransferServiceClient();
//FileTransferClient.FileTransferServiceClient client = new FileTransferClient.FileTransferServiceClient();
// upload file
client.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);
// close service client
client.Close();
}
}
return true;
}).ContinueWith(r =>{
if(r.Result) LogText("Done!!");
});
}
If I leave this and try to compile I get:
"Cannot assign void to an implicitly-typed local variable"** in the
var someTask
So I then change var someTask
to Task someTask
but now I get the error:
"Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'".
Any clue on how to deal with this?
Upvotes: 0
Views: 2134
Reputation: 887453
The await
keyword waits for a task to finish and returns the result.
It does not return the task itself.
Since your ContinueWith()
doesn't have a result (it returns Task
, not Task<T>
), your await
expression returns void
, which obviously cannot be assigned to a variable.
You can simplify your code like this:
await Task.Run(() => {
using (...) {
...
}
});
LogText("Done!");
Upvotes: 6