Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync/Await with a WinForms ProgressBar - WinForms version
    primarykey
    data
    text
    <p>Please see: <a href="http://codepaste.net/djw3cw" rel="nofollow noreferrer">http://codepaste.net/djw3cw</a> for the code</p> <p>If it's true that async/await for asynchronous programming will soon be like Linq, I think this question is a non-trivial extension of <a href="https://stackoverflow.com/questions/17972268/async-await-with-a-winforms-progressbar">Async/Await with a WinForms ProgressBar</a> </p> <p>I would be greatful for a pointer or an answer in words in lieu of code, though code is optimal.</p> <p>The issue is this: how to set up a progress bar to work using asynch/await. In the past I've used a Dispatcher successfully.</p> <pre><code>Please see: http://codepaste.net/djw3cw for the code What is done: a textbox has any text in it converted to an int, then when "mybutton1" is clicked, work is done based on the int, for int ms (int = milliseconds). During this time, a progressbar "myProgressBar" is shown for every tenth-percent step When work is complete, the label/textblock controls are updated But the below does not work right: the form simply freezes until the work is complete. How to fix it? How to do this using Task-based Asynchronous Pattern (TAP) or Async/Await, rather than a Dispatcher? </code></pre> <p>Here is a snippet of the code in question, self-explanatory. </p> <pre><code> private void mybutton1_Click(object sender, RoutedEventArgs e) { myLabel.Content = myTextBox.Text; string myString = myTextBox.Text; bool result = Int32.TryParse(myString, out myInteger); if (result == true) { myTextblock.Text = "Success, thread will be delayed by: " + myInteger.ToString() + " ms"; // int CounterInteger = 0; for (int j = 0; j &lt; myInteger; j++) // set # itt { Thread.Sleep(1); //do some work here // myClassDoWork1.Delay_DoWork(myInteger); //optional way to do work in another class... if (j % (myInteger / 10) == 0) //display progress bar in 10% increments { CounterInteger = CounterInteger + 10; myProgressBar.Value = (double)CounterInteger; // won't work here in a parallel manner...must use Dispatcher.BeginInvoke Action //how to make this work using Async/Await? see: https://stackoverflow.com/questions/17972268/async-await-with-a-winforms-progressbar } } /// /// // above does not work properly: the form simply freezes until the work is complete. How to fix it? /// myLabel.Content = "done, delayed work done successfully: in " + myInteger.ToString() + " milliseconds"; myTextblock.Text = "done, delayed work done successfully: in " + myInteger.ToString() + " milliseconds"; return; } else { myTextblock.Text = "Error, integer not entered, try again." + myTextBox.Text; myLabel.Content = "Error, integer not entered, try again."; return; } } </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.
 

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