wpf TextBlock 不会更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12036600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextBlock Will Not Update
提问by Robbo905
I have a text block called "findListText". Here, I am updating the text in it:
我有一个名为“findListText”的文本块。在这里,我正在更新其中的文本:
private void InstantSearch(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
HitEnter = true;
}
findListText.Text = "Processing request. Please wait...";
Find(bool.Parse("False" as string));
}
However, the next set of code is a search function that can take up to 10 seconds, and at the end of it, it changes the text in findListText again.
但是,下一组代码是一个搜索功能,最多可能需要 10 秒,并且在它结束时再次更改 findListText 中的文本。
private void Find(bool? bForward = true)
{
{
//Lots and lots of code
}
findListText.Text = "Search completed."
}
The problem is, the textblock never seems to update to "Processing request. Please wait...". The textblock is in it's original state and 10 seconds later updates to "Search completed.", seemingly skipping out the middle man.
问题是,文本块似乎永远不会更新为“正在处理请求。请稍候...”。文本块处于其原始状态,10 秒后更新为“搜索完成。”,似乎跳过了中间人。
I'm using C# - WPF. What am I doing wrong here?
我正在使用 C# - WPF。我在这里做错了什么?
回答by Terry
Doesn't matter what technology I think.
我认为什么技术并不重要。
The code is running on the same thread, meaning the the UI won't be updated untill all the code on that thread is completed. You should address a different thread to update that textblock.
代码在同一个线程上运行,这意味着在该线程上的所有代码完成之前不会更新 UI。您应该解决一个不同的线程来更新该文本块。
In that case, you will have 2 thread:
在这种情况下,您将有 2 个线程:
- The origininal thread, executing the "lots and lots of code"
- The second (extra) created thread, which will handle updating the textblock's text while the other thread is executing the other code.
- 原始线程,执行“大量代码”
- 第二个(额外)创建的线程将在另一个线程执行其他代码时处理更新文本块的文本。
I've created a little somethingthat should resolve your problem, it's based on thisStack Overflow page
回答by Aphelion
You should look into the UI threading concept of WPF. Invoke the Dispatcherto modify the textbox. Also the search should run with ThreadPool.QueueWorkerItem.
您应该研究 WPF 的 UI 线程概念。调用Dispatcher来修改文本框。此外,搜索应该使用ThreadPool.QueueWorkerItem.
// Start worker thread
ThreadPool.QueueUserWorkItem(state =>
{
// Long running logic here
findListText.Dispatcher.BeginInvoke(() => findListText.Text = "Processing request. Please wait...");
Find(bool.Parse("False" as string));
// Tip: Run change on GUI thread from the worker using the dispatcher
findListText.Dispatcher.BeginInvoke(() => findListText.Text = "Search completed.");
});
回答by Andre Calil
Since this is WPF, try the following: after changing the text to "Processgin", call:
由于这是 WPF,请尝试以下操作:将文本更改为“Processgin”后,调用:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { this.UpdateLayout(); }));
This will tell the thread to update the UI as soon as possible.
这将告诉线程尽快更新 UI。
回答by steveg89
Here is how to run your find method in its own thread.
以下是如何在自己的线程中运行您的 find 方法。
private void InstantSearch(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
HitEnter = true;
}
findListText.Text = "Processing request. Please wait...";
BackgroundWorker tempWorker = new BackgroundWorker();
tempWorker.DoWork += delegate
{
Find(bool.Parse("False" as string));
};
tempWorker.RunWorkerAsync();
}
If you try that, you'll get an error because you access your UI thread from the background thread. So you'll need to update your find method as well.
如果你尝试这样做,你会得到一个错误,因为你从后台线程访问你的 UI 线程。因此,您还需要更新您的 find 方法。
private void Find(bool? bForward = true)
{
{
//Lots and lots of code
}
Dispatcher.BeginInvoke((Action) delegate {
findListText.Text = "Search completed."
});
}
回答by Robbo905
I came back to this just now, and had another browse across the internet for similar problems. For something as simple as pushing a single message before a long running process occurs, I'm surprised no-one suggested "Application.DoEvents();". Yes, I know it has it's flaws, and my UI will still hang, but this suits me perfectly for my situation, at least.
我刚刚回到这个问题,并在互联网上再次浏览了类似的问题。对于在长时间运行的进程发生之前推送一条消息这样简单的事情,我很惊讶没有人建议“Application.DoEvents();”。是的,我知道它有缺陷,我的 UI 仍然会挂起,但这至少适合我的情况。

