如何在WPF循环期间实时更新控制台? [英] How to I update a console real time during a loop in WPF?

查看:107
本文介绍了如何在WPF循环期间实时更新控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在相当冗长的导入过程中显示用户的进度消息。每当Importer引发进度事件时,我都尝试写入TextBox,但即使在循环结束后,文本框仍保持空白。然后我尝试使用ListBox,为每个进度事件添加一个新项。没有不同。我可以看到事件被捕获并阅读文本,但是无法正确更新我的进度显示。

I'm trying to display progress messages for the user during a fairly lengthy import process. I tried writing to a TextBox whenever the Importer raises a progress event, but the textbox remains blank, even after the loop finishes. Then I tried using a ListBox, adding a new item for each progress event. No difference. I can see the event beinh caught and read the text, but just can't get it right to update my progress display.

推荐答案

这与无关即时的。我认为所有实现CLR的系统都不是实时操作系统。让我们忘掉吧。



也许你只是在运行一个额外的线程(不是UI线程),并希望UI在循环中得到更新。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
This has nothing to do with "real time". I think all systems where CLR is implemented are not real-time OS. Let's forget it.

Perhaps you are just running an additional thread (not UI thread) and want UI to get updated in a loop. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何在WPF循环期间实时更新控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆