C# 与屏幕更新同步

发布于 2024-10-17 00:28:11 字数 1095 浏览 1 评论 0原文

我有以下问题: 我的应用程序进行屏幕更新,并应在之后生成新视图的屏幕截图。
代码如下:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-10-24 00:28:11

当我必须做类似的事情时,我总是使用计时器事件,该事件将在处理完所有内容后触发。这将是一次性计时器事件,用法如下:

要触发它:

Timer t=new Timer();
t.Tick+=DoAfter;
t.Interval=100;
t.Start();

需要做的事情在这里:

void DoAfter(object Sender, SomeArgs a)
{
    Timer t=(Timer)Sender;
    t.Stop();
    t.Dispose();
    //  perform your stuff here
    DoStuff();
}

这样,您就有很好的机会消息循环将完成所有操作,并且屏幕更新将在计时器触发之前完成。

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:

Timer t=new Timer();
t.Tick+=DoAfter;
t.Interval=100;
t.Start();

What needs to be done is here:

void DoAfter(object Sender, SomeArgs a)
{
    Timer t=(Timer)Sender;
    t.Stop();
    t.Dispose();
    //  perform your stuff here
    DoStuff();
}

That way you have a good chance that message loop will do everything and the screen updates will finish BEFORE timer fires.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文