并行。用于更新文本块

发布于 2024-10-15 02:46:31 字数 524 浏览 10 评论 0原文

我正在尝试从 Parallel.For 中更新 WPF 文本块,但不能。我使用调度程序,但我想我的方式是错误的。所有工作首先完成,然后文本块快速迭代更新。这是我的代码:

Parallel.For(0, currentScene.Textures.Count, delegate(int i)
       {

           TextureObject texture = (currentScene.Textures[i]);

           MainWindow.Instance.StatusBarText.Dispatcher.BeginInvoke(new Action(()
               => MainWindow.Instance.StatusBarText.Text = "Loading Texture " + i
               + " - " + texture.Name ), null);
           LoadTexture(texture);
           }
       });

I am trying to update a WPF textblock from within a Parallel.For and I can't. I use a dispatcher but I guess, I do it the wrong way. All of the work is done at first and then the textblock updates iteratively and fast. Here's my code:

Parallel.For(0, currentScene.Textures.Count, delegate(int i)
       {

           TextureObject texture = (currentScene.Textures[i]);

           MainWindow.Instance.StatusBarText.Dispatcher.BeginInvoke(new Action(()
               => MainWindow.Instance.StatusBarText.Text = "Loading Texture " + i
               + " - " + texture.Name ), null);
           LoadTexture(texture);
           }
       });

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

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

发布评论

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

评论(3

忆依然 2024-10-22 02:46:31

Parallel.For 调用本身是在您的 UI 线程上进行的,阻止该线程更新,直到调用返回。相反,请执行此操作:

    Task.Create(delegate   
    {   
       Parallel.For( /* your current code */ );
    });   

BackgroundWorker 类可能是这种情况下更合适的解决方案...

参考:http://social.msdn.microsoft.com/Forums/en/parallelextensions/thread/38d7a436-e1d1-4af8-8525-791ebeed9663

The Parallel.For call is itself being made on your UI thread, blocking that thread from updating until the call returns. Do this instead:

    Task.Create(delegate   
    {   
       Parallel.For( /* your current code */ );
    });   

The BackgroundWorker class may be a more appropriate solution for this scenario though...

Reference: http://social.msdn.microsoft.com/Forums/en/parallelextensions/thread/38d7a436-e1d1-4af8-8525-791ebeed9663

感情洁癖 2024-10-22 02:46:31

罗伯特是对的,但我会这样写:

Enumerable.Range(0, currentScene.Textures.Count).Select(i =>
    new Task(() => {
       TextureObject texture = (currentScene.Textures[i]);

       MainWindow.Instance.Dispatcher.BeginInvoke(new Action(()
           => MainWindow.Instance.StatusBarText.Text = "Loading Texture " + i
           + " - " + texture.Name ), null);
       LoadTexture(texture);
    });
).Run(x => x.Start());

不需要创建一个其唯一工作就是等待其他任务的任务。

Robert's right, but here's how I would write it:

Enumerable.Range(0, currentScene.Textures.Count).Select(i =>
    new Task(() => {
       TextureObject texture = (currentScene.Textures[i]);

       MainWindow.Instance.Dispatcher.BeginInvoke(new Action(()
           => MainWindow.Instance.StatusBarText.Text = "Loading Texture " + i
           + " - " + texture.Name ), null);
       LoadTexture(texture);
    });
).Run(x => x.Start());

No need to create a Task whose sole job is to sit and wait on other Tasks.

撩起发的微风 2024-10-22 02:46:31

正如 Levy 先生指出的那样,对 Parallel.For() 的任何调用都将是阻塞调用,直到所有循环迭代完成为止。因此,您可以执行上面建议的操作,或者简单地将调用包装在后台线程中。

ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object arg)
{
    Parallel.For(0, currentScene.Textures.Count, delegate(int i)        
    {
        // The rest of your code .....
    }
}));

As Mr. Levy points out any call to Parallel.For() will be a blocking call until all the loop iterations have completed. As such, you can do what was suggested above or simply wrap the invocation in a background thread.

ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object arg)
{
    Parallel.For(0, currentScene.Textures.Count, delegate(int i)        
    {
        // The rest of your code .....
    }
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文