如何在处理时更新 silverlight UI

发布于 2024-09-08 20:13:09 字数 790 浏览 5 评论 0原文

我浏览了网上发布的几个例子,但我无法回答我的问题。

我的“p”变量在 for 循环中增加了 1。我希望 UI 显示计算进度(以显示“p”如何从 0 增加到 1000000)。我在单独的线程上进行计算,并调用调度程序来更新 UI 中的 ResultBox。示例:

 int p=0;

 ...

 private void GO(object sender, System.Windows.RoutedEventArgs e)
 {
        new Thread(delegate()
        {
            DoWork();
        }).Start();

}

    void DoWork()
    {
        for (int i = 0; i < 1000; i++)
        {
            for (int j = 0; j < 10000; j++)
            {
                p++;
                this.Dispatcher.BeginInvoke(delegate { ResultBox.Text = p.ToString(); });
            }
        }

    }

由于某种原因,这不起作用。但是,当我将 Thread.Sleep(1) 放在 this.Dispatcher... 之前时,它会按预期工作。这是否意味着 UI 更新(Dispatcher)调用过于频繁,因此冻结? 还有其他方法吗?

谢谢

I went through several examples posted online but I cant answer my question.

I have my 'p' variable that is being increased by 1 in the for loop. I want the UI to display the progress of calculation (to show how 'p' is increasing from 0 to 1000000). I do the calculation on the separate thread and the I call dispatcher to update the ResultBox in UI. Example:

 int p=0;

 ...

 private void GO(object sender, System.Windows.RoutedEventArgs e)
 {
        new Thread(delegate()
        {
            DoWork();
        }).Start();

}

    void DoWork()
    {
        for (int i = 0; i < 1000; i++)
        {
            for (int j = 0; j < 10000; j++)
            {
                p++;
                this.Dispatcher.BeginInvoke(delegate { ResultBox.Text = p.ToString(); });
            }
        }

    }

For some reason this doesn't work. However when I put Thread.Sleep(1) just before this.Dispatcher... it works as intended. Does it mean that the UI update (Dispatcher) is called too frequently therefore it freezes?
Is there any other way to do it?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丘比特射中我 2024-09-15 20:13:09

为什么不将属性绑定到文本框并更新属性值,而不是直接点击文本框?

Why not bind a property to your TextBox and the update the property value instead of poking at the textbox directly?

蒲公英的约定 2024-09-15 20:13:09

是的,仅在循环中执行 p++ 不会花费太多时间,并且在 silverlight 内部,Dispatcher 只是一个带有委托的简单队列,并且在 silverlight 甚至可以更新和处理其 UI 之前,您在队列上注入了太多值。想象一下,如果您继续以更快的速度添加队列,然后队列出队,最终它也会达到最大限制,会发生什么。最终它会停止。如果你的 p++ 被替换为更耗时的任务,那么你可能会得到好的结果。

你必须知道,我们的眼睛通常只能看到 30 fps 的更新,每秒超过 30 次更新根本没有任何用处,我建议你的视图更新应减少到每秒最多 10 次更新,以获得最佳性能。

为了显示进度,我认为每秒更新 1 次也足够了。首先总是非常缓慢地显示更新,例如

void DoWork() 
{ 
    for (int i = 0; i < 1000; i++) 
    { 
        for (int j = 0; j < 10000; j++) 
        { 
            p++; 
            if((p % 1000)==0){
                 this.Dispatcher.BeginInvoke(delegate 
                      { ResultBox.Text = p.ToString(); });
            } 
        } 
    } 

} 

现在您可以增加/减少 1000 到 10 的某个合适的倍数来调整视觉更新。

Yes only doing p++ in your loop will not take much of time and inside silverlight, Dispatcher is nothing but a simple queue with delegates, and before silverlight can even update and process its UI, you are pumping too many values on the queue. Imagin what will happen if you keep on adding queue way to faster then the queue is dequeued, then eventually it will hit max limit as well. And eventually it will just stop. If your p++ is replaced with more time consuming task, then you may get good result.

You must know that our eye usually can see only updates of 30 fps, more then 30 updates per second will not be of any use at all, I will suggest your view update should be reduced to max 10 updates per second for best performance.

And for showing progress, I think 1 update per second is also enough. First always display updates very slowly, like

void DoWork() 
{ 
    for (int i = 0; i < 1000; i++) 
    { 
        for (int j = 0; j < 10000; j++) 
        { 
            p++; 
            if((p % 1000)==0){
                 this.Dispatcher.BeginInvoke(delegate 
                      { ResultBox.Text = p.ToString(); });
            } 
        } 
    } 

} 

Now you can increaes/decrease 1000 to some suitable multipler of 10 to adjust your visual update.

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