【发布时间】:2023-03-09 15:05:02
【问题描述】:
代码请见:http://codepaste.net/djw3cw
如果异步编程的 async/await 真的很快就会像 Linq 一样,我认为这个问题是 Async/Await with a WinForms ProgressBar 的重要扩展
我会很高兴用指针或文字代替代码回答,尽管代码是最佳的。
问题是:如何设置进度条以使用 asynch/await 工作。过去我曾成功使用过 Dispatcher。
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?
这是有问题的代码的 sn-p,不言自明。
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 < 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;
}
}
【问题讨论】:
-
请将代码编辑到您的问题中 - 理想情况下编辑得尽可能短,同时仍然完整并展示问题。
-
嗨,乔恩——我实际上正在阅读你关于“C# 深度学习”这一主题的书。您在 CodePaste 上看到代码了吗?我发现这里剪切和粘贴代码对我来说不是很好,抱歉。
-
是的,我可以在 CodePaste 上看到代码 - 但它根本不是提问的好方法。熟悉 Stack Overflow 降价编辑器(和预览窗口)是值得的 - 清晰地格式化您的问题(而不仅仅是链接到场外代码)对于创建一个好问题很重要。
-
好的,让我再试一次...
标签: c# asynchronous progress-bar async-await