代码在控件可视化地绘制到屏幕之前执行,从而阻止它被绘制 [英] Code is executing before a control is visually drawn to the screen stopping it ever getting drawn

查看:62
本文介绍了代码在控件可视化地绘制到屏幕之前执行,从而阻止它被绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有组合框和一些其他控件的窗口,组合框将xml文件中的数据加载到标签中以显示给用户。加载的xml根据组合框的选定索引而变化。我也有一个用户控件,我看起来像一个启动画面,但它是长时间操作,所以它有一个指南针的图像(不要问为什么)和文字说'加载。 。 。每当用户选择组合框中的项目时,我希望用户控件在整个窗口中可见,因此它覆盖它(它已经就位,因此只需要更改可见性)。然后操作将完成,用户控件将被隐藏。



看起来很简单,我正在改变组合框的selectionchanged事件的可见性但是它也读取大型xml文件的位置。我遇到的问题是代码执行正常,但是当可见性发生变化时,它没有足够的时间来绘制usercontrol。因此,它会改变可见性,并认为它在屏幕上可见,并且当实际上usercontrol的属性设置为可见但实际图形尚未被绘制到屏幕时移动到文件读取,因此它删除了目的有等待屏幕因为它从未被绘制过。我怎样才能避免这种情况,所以当索引发生变化时,用户控件会在移动之前被绘制到屏幕上?告诉线程进入睡眠状态是行不通的,因为它会在认为它被绘制后暂停线程,因此它只会延迟xml读取的完成。我希望我没有解释得太多,谢谢。

I have a window with a combobox and a few other controls on and the combobox loads data from an xml file into labels to show to the user. The xml that is loaded changes depending on the selected index of the combobox. I also have a user control that I have made to look like a splash screen but it is for long operations so it has an image of a compass (don't ask why) and text to say 'Loading . . .'. Whenever the user selects an item in the combobox I want the usercontrol to become visible over the whole of the window so it covers it (it is already in place so the visibility just needs to be changed). Then the operation will complete and the usercontrol will become hidden.

It seems simple to do and I am currently changing the visibility on the selectionchanged event of the combobox but that it also where the large xml file is read. The problem I am having is that the code is executed fine but it does not have enough time to draw the usercontrol when the visibility is changed. SO it changes the visibility and thinks that it is then visible on the screen and moves on to the file reading when in fact the usercontrol's property is set to visible but that actual graphics has not been drawn to the screen so it then removes the purpose of having that waiting screen because it is never drawn. How can I avoid this so when the index is changes, the usercontrol is drawn to the screen before it moves on? Telling the thread to sleep would not work because it would be pausing the thread after it thinks it is drawn so it would only delay the finishing of the xml reading. I hope I have not explained it too much, thank you.

推荐答案

听起来这是标准的GUI线程饥饿。



当您将用户控件设置为可见时,它不会立即执行此操作。而是将请求发布到消息队列中。读取这个并处理请求的线程是读取xml文件的同一个线程,但它实际上不能这样做,因为它正在忙着读取xml。



你应该做的是在后台线程上读取xml(或者需要花费很多时间的东西)。这使GUI线程可以自由地实际处理GUI内容。看一下BackgroundWorker类 - 这对于你想要做的事情来说是完美的。
It sounds like this is standard GUI thread starvation.

When you set the user control visible, it doesn't do it straight away. Instead the request gets posted onto a message queue. The thread which would read this and process the request is the same thread which is reading the xml file, but it can't actually do that because its busy reading the xml instead.

What you should do is the reading of the xml (or anything which takes more than a brief moment) on a background thread. This leaves the GUI thread free to actually attend to GUI stuff. Take a look at the BackgroundWorker class - this will be perfect for what you are trying to do.


这篇关于代码在控件可视化地绘制到屏幕之前执行,从而阻止它被绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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