C# WPF 调度程序 - 无法正确执行
我正在尝试更改 UI,然后让我的函数运行 这是我的代码:
private void btnAddChange_Document(object sender, RoutedEventArgs e)
{
System.Threading.ThreadStart start = delegate()
{
// ...
// This will throw an exception
// (it's on the wrong thread)
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(changest));
//this.BusyIndicator_CompSelect.IsBusy = true;
//this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
};
// Create the thread and kick it started!
new System.Threading.Thread(start).Start();
}
public void changest()
{
this.BusyIndicator_CompSelect.IsBusy = true;
this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
t = "Creating document 1/2..";
}
我想在 ui 更新后/ ThreadStart“启动”结束后运行的函数:
string x = "";
for(int i =0;i<=1000;i++)
{
x+= i.ToString();
}
MessageBox.Show(x);
那么我应该做什么?
I am trying to make a change in the UI and then make my function run
here's my code:
private void btnAddChange_Document(object sender, RoutedEventArgs e)
{
System.Threading.ThreadStart start = delegate()
{
// ...
// This will throw an exception
// (it's on the wrong thread)
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(changest));
//this.BusyIndicator_CompSelect.IsBusy = true;
//this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
};
// Create the thread and kick it started!
new System.Threading.Thread(start).Start();
}
public void changest()
{
this.BusyIndicator_CompSelect.IsBusy = true;
this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
t = "Creating document 1/2..";
}
the function I want to run after the ui updates / after the ThreadStart 'start' has ended:
string x = "";
for(int i =0;i<=1000;i++)
{
x+= i.ToString();
}
MessageBox.Show(x);
So what am I supposed to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您想异步执行一些操作。正确的?为此,我建议在 WPF 中使用 BackgroundWorker-班级:
我希望这就是你的问题。
I assume you want to execute some action asynchronous. Right? For this, I recommend using in WPF the BackgroundWorker-class:
I hope, this is what your question is about.
对你想要实现的目标有点困惑,但我相信这就是你想要的......
Slightly confused on what you are trying to accomplish but I believe this is what you are after...