【发布时间】:2014-07-29 10:31:32
【问题描述】:
是否可以在 Windows 窗体应用程序中执行类似的操作?
我正在尝试寻找其他方法来更新 UI,而不是一直使用 BackgroundWorker。也许是这样的?:
public List<String> results = new List<String>();
private void button1_Click(object sender, EventArgs e)
{
When(SomeLongRunningMethod("get") == true)
{
// LongRunningMethod has completed.
// display results on Form.
foreach(string result in results)
{
this.Controls.Add(new Label() { Text = result; Location = new Point(5, (5 * DateTime.Now.Millisecond)); });
}
}
}
public void SomeLongRunningMethod(string value)
{
if(value == "get")
{
// Do work.
results.Add("blah");
}
}
上面基本上说,“做这个,当你完成后,将结果添加到表单中。”
【问题讨论】:
-
查看异步/等待。完全满足您的需求。