Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrying asynchronous file upload on error
    primarykey
    data
    text
    <p>I'm trying to upload files in a WPF-application. Everything works fine if the server responds but the application will be used in an environment with "unsafe" internet connection. So I want to retry uploading after a short break if the first attempt failed.</p> <p>I tried several things with async/await and ended up with the following code. If the server is running everything is fine, but if not the program fails with an ObjectDisposedException in the second iteration of the while-loop.</p> <p>Any ideas?</p> <pre><code>private void UploadButton_Click(object sender, RoutedEventArgs e) { // build content to send content = new MultipartFormDataContent(); var filestream = new FileStream(filePath, FileMode.Open); var fileName = System.IO.Path.GetFileName(filePath); content.Add(new StreamContent(filestream), "file", fileName); content.Add(new StringContent(terminal_id.ToString()), "terminal_id"); UploadTask(content); /*var task_a = new Task(() =&gt; UploadTask(content)); task_a.Start();*/ } private async void UploadTask(HttpContent content) { bool success = false; int counter = 0; while (counter &lt; 3 &amp;&amp; !success) { Debug.WriteLine("starting upload"); success = await UploadFileAsync(content); Debug.WriteLine("finished upload. result " + success.ToString()); //if (!success) System.Threading.Thread.Sleep(5000); counter++; } } private async Task&lt;bool&gt; UploadFileAsync(HttpContent content) { var message = new HttpRequestMessage(); message.Method = HttpMethod.Post; message.Content = content; message.RequestUri = new Uri(target_url); using (HttpClient client = new HttpClient()) { try { HttpResponseMessage res = await client.SendAsync(message); if (res.IsSuccessStatusCode) return true; } catch (HttpRequestException hre) { Debug.WriteLine(hre.ToString()); } return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload