0

I'm writing a C# app in Visual Studio 2017 and want to use UploadFileTaskAsync instead of UploadFile but I'm going to be sending up 50+ files in the run. How do I know when it's complete? I'm very new to C# so I'm learning by searching and trial/error so I apologize if this is a poorly formed question.

I found posts about UploadFileTaskAsync and it's usage but nothing about how it can be setup to notify the app that it's done.

Here's the code I'm using (that I found):

byte[] response = await webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

I'm doing byte[] response instead of Task<byte[]> response because VS doesn't like byte[] in Task<> for UploadFileTaskAsync.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • Hi Marty, welcome to SO. Please learn how to properly format your questions so that others can help you more easily: https://stackoverflow.com/help/how-to-ask – petezurich Jul 02 '17 at 05:32
  • `webClient.UploadFileTaskAsync` returns `Task` Using `await` means your program block until execute finishes. And is probably why you can't useT ask. What do you mean by notify the app it is done? Do you want to run the method asynchronous? – Alexander Higgins Jul 02 '17 at 05:36
  • 2
    @AlexanderHiggins probably the "block" word is too strong there. It may make someone think that it blocks, while it does not. – zerkms Jul 02 '17 at 05:37
  • @Marty you don't need to "get notified": at the point the variable assignment is happening - the result is already known, it's all done thanks to the `await`. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/await – zerkms Jul 02 '17 at 06:01
  • Start with the marked duplicate. Then if you still don't understand, read more documentation and tutorials about `async`/`await`. You didn't provide any context, so there's no way to really explain this in relation to the code you have, but: your call to `UploadFileTaskAsync()` is being "awaited", that is you're using `await`, which means the next line of code after that won't execute until the operation completes. If it can't be completed right away (which is likely in this case), the method in which the call is contained will return, and be resumed later, when the operation does complete. – Peter Duniho Jul 02 '17 at 06:36
  • So, whatever you want to do when you're "notified", you just put in the same method, after the statement that executes the `await`. – Peter Duniho Jul 02 '17 at 06:36

0 Answers0