简体   繁体   English

单独窗口中的进度条

[英]progress bar in separate window

(WPF) We have been asked to build a little window that pops up when the app is busy (instead of or in addition to a wait cursor). (WPF)我们被要求构建一个小窗口,当应用程序忙时(代替等待光标或除等待光标之外),该窗口会弹出。 We put a textblock in the window, and a ProgressBar with IsIndeterminate = true. 我们在窗口中放置一个文本块,并使用IsIndeterminate = true的ProgressBar。

We call show on the little window, and then start up our long-running process, calling Close on the little window when we are through. 我们在小窗口上调用show,然后开始长时间运行的过程,在浏览通过时在小窗口上调用Close。 However, during the long-running process, the ProgressBar does not show any activity, and the little window shows (Not Responding) in its title. 但是,在长时间运行的过程中,ProgressBar不会显示任何活动,并且小窗口的标题会显示(无响应)。

Is this even possible? 这有可能吗? A better idea? 有更好的主意吗?

You need to look into using another thread (or multiple threads) to do the heavy processing that could take longer than 100ms (100ms and above can cause this 'hanging' appearence before 'Not Responding' appears.) 您需要研究使用另一个线程(或多个线程)来进行繁重的处理,该处理可能会花费超过100毫秒的时间(100毫秒及以上的时间会导致这种“挂起”的出现,然后再出现“未响应”。)

You could create a new thread using a BackgroundWorker object and subscribe to the OnProgressChanged event to indicate a progress bar update. 您可以使用BackgroundWorker对象创建一个新线程,并订阅OnProgressChanged事件以指示进度条更新。 This does not however get around the problem of accessing the main (UI) threads Progress Bar. 但是,这不能解决访问主(UI)线程进度栏的问题。 You would have to check if an Invoke is required and then invoke a piece of code responsible for updating the Progress Bar. 您将必须检查是否需要调用,然后调用一段代码来更新进度条。

Here is a StackOverflow question that shows a nice Extension Method in the accepted answer for invoking a method call on a control: 这是一个StackOverflow问题,在接受的答案中显示了一个不错的扩展方法,用于调用控件上的方法调用:

Invoking 调用

Hopefully that helps!! 希望有帮助!!

The problem is most likely that whatever is causing the app to be "busy" is being run in the main thread of the app. 问题很可能是导致应用程序“繁忙”的任何原因都在应用程序的主线程中运行。 So, the app stops responding, including responding to layout and painting requests from the OS. 因此,该应用程序停止响应,包括响应来自操作系统的布局和绘画请求。 So, when the app is "busy", it's too busy to paint the new window. 因此,当应用程序“繁忙”时,它太忙而无法绘制新窗口。

The solution is to move as much "heavy lifting" as possible into background threads, so the main thread and thus the UI remain responsive. 解决方案是将尽可能多的“繁重工作”移入后台线程,以便主线程和UI保持响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM