C# 与屏幕更新同步
我有以下问题: 我的应用程序进行屏幕更新,并应在之后生成新视图的屏幕截图。
代码如下:
public ViewModelBase Screen {
get {
if (_screen == null) {
Screen = new DataSourceChooserScreenViewModel(this);
History.ChangeHistory(ADD_ITEM);
}
return _screen;
}
set {
_screen = value;
OnPropertyChanged("Screen");
System.Windows.Forms.Application.DoEvents();
}
}
在“OnPropertyChanged("Screen")”中进行屏幕更新,在“History.ChangeHistory(ADD_ITEM)”中进行截图。
调用“Screen”设置器的方法都遵循相同的方案:
//Do something
Screen = otherScreen;
History.ChangeHistory(ADD_ITEM);
//Do something else
问题如下: 尽管执行顺序另有说明,但屏幕截图是在屏幕更新之前制作的。
为了自己解决问题,我尝试了几种方法:
正忙于在 ADD_ITEM 中等待
在 ADD_ITEM 中通过计时器等待
通过 BeginInvoke() 和 EndInvoke() 使用异步线程等待 PropertyChangedEvent
System.Windows.Forms.Application.DoEvents() 的用法
所有尝试都有相同的结果:屏幕截图是在屏幕更新之前完成的。在尝试使用我发现的计时器时,屏幕会在执行 ADD_ITEM 之后更新。所以我建议编译器进行一些隐式线程处理,但我不知道如何修复/防止它。
感谢您的帮助。
I have the following Problem:
My application does a screen-update and should make a screenshot of the new view after that.
The code is as follows:
public ViewModelBase Screen {
get {
if (_screen == null) {
Screen = new DataSourceChooserScreenViewModel(this);
History.ChangeHistory(ADD_ITEM);
}
return _screen;
}
set {
_screen = value;
OnPropertyChanged("Screen");
System.Windows.Forms.Application.DoEvents();
}
}
The screen update is done in "OnPropertyChanged("Screen")" and the screenshot is taken in "History.ChangeHistory(ADD_ITEM)".
The methods calling the setter for "Screen" follow all the same scheme:
//Do something
Screen = otherScreen;
History.ChangeHistory(ADD_ITEM);
//Do something else
And here's the problem:
The screenshot is made before the screen has been updated although the execution order says something else.
To solve the problem by myself I tried several things:
Busy waiting in ADD_ITEM
Waiting by timer in ADD_ITEM
Usage of asynchronous threading with waiting on PropertyChangedEvent via BeginInvoke() and EndInvoke()
Usage of System.Windows.Forms.Application.DoEvents()
All tries had the same result: The screenshots was done before the screen became updated. In the attempt with the usage of timers I figured out, the screen becomes updated after ADD_ITEM was performed. So I suggest the compiler makes some implicit threading and I don't know how to fix/prevent it.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我必须做类似的事情时,我总是使用计时器事件,该事件将在处理完所有内容后触发。这将是一次性计时器事件,用法如下:
要触发它:
需要做的事情在这里:
这样,您就有很好的机会消息循环将完成所有操作,并且屏幕更新将在计时器触发之前完成。
When I have to do something like that, I always use timer event that will fire AFTER everything is processed. It would be one-shot timer event and usage will be something like this:
To fire it:
What needs to be done is here:
That way you have a good chance that message loop will do everything and the screen updates will finish BEFORE timer fires.