并行编程:无法访问UI并行? [英] Parallel Programming: Can't access UI Parallel?

查看:96
本文介绍了并行编程:无法访问UI并行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在WPF C#中的函数也运行在UI操作的并行执行。但运行时存在总是在方法上的UI控件一个例外:因为不同的线程拥有它调用线程不能访问该对象。

例外总是叫上环正在运行的第二个实例,所以不可能操纵两个并行运行的情况下,UI?

是否有可能?存取权限并联UI



代码:

 
{
如果(listBox_Copy.SelectedIndex< listBox_Copy.Items.Count - 1)
{
listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
listBox_Copy.ScrollIntoView(listBox_Copy.SelectedItem);
} listBox_Copy.Focus();
huidig​​item = listBox_Copy.SelectedItem为ListBoxItem的;
currentitemindex = listBox_Copy.SelectedIndex;
CURRENTITEM = listBox_Copy.ItemContainerGenerator.ContainerFromIndex(currentitemindex)为ListBoxItem的;
itemgrid = FindVisualChild<网格和GT;(CURRENTITEM);
senderbutton =(按钮)发送;
键playcues = itemgrid.FindName(PLAYBUTTON)作为按钮;
cuetrigger = itemgrid.FindName(cuetrigger)作为TextBlock的;
Jobs.Add(playcues);
},而(cuetrigger.Text =走出去!);
Parallel.ForEach(乔布斯playcues => {播放(playcues,新RoutedEventArgs());});

}



然后崩溃的播放功能



 私人无效播放(对象发件人,System.Windows.RoutedEventArgs E)
{电网itemgrid = VisualTreeHelperExtensions.FindAncestor<网格和GT;(playcue );


解决方案

这是不可能从后台访问UI线程,所有更新必须在主线程上。您可以通过使用分派做到这一点。



像这样

 操作X =(动作){委托
//做我的UI更新
};
Dispatcher.Invoke(X,新的对象[] {});


I'm trying to create parallel execution of a function in wpf c# which also runs actions on the UI. But when running there is always an exception at methods on UI Controls: The calling thread cannot access this object because a different thread owns it. The exception is always called on the second instance of the loop being run, so it isn't possible to manipulate the UI in two parallel running instances?

Is it possible to acces the UI in parallel?

Code:

                do
                {  
                    if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count - 1)
                    {
                        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
                        listBox_Copy.ScrollIntoView(listBox_Copy.SelectedItem);
                    } listBox_Copy.Focus();
                    huidigitem = listBox_Copy.SelectedItem as ListBoxItem;
                    currentitemindex = listBox_Copy.SelectedIndex;
                    currentitem = listBox_Copy.ItemContainerGenerator.ContainerFromIndex(currentitemindex) as ListBoxItem;
                    itemgrid = FindVisualChild<Grid>(currentitem);
                    senderbutton = (Button)sender;
                    Button playcues = itemgrid.FindName("Playbutton") as Button;
                    cuetrigger = itemgrid.FindName("cuetrigger") as TextBlock;
                    Jobs.Add(playcues);
                } while (cuetrigger.Text != "go");
                Parallel.ForEach(Jobs, playcues => { play(playcues, new RoutedEventArgs()); });

            }

And then it crashes at the play function

            private void play(object sender, System.Windows.RoutedEventArgs e)
            {  Grid itemgrid = VisualTreeHelperExtensions.FindAncestor<Grid>(playcue);

解决方案

It is not possible to access the UI from a background thread, all your updates must be on the main thread. You can do this by using the Dispatcher

Something like this

        Action x = (Action)delegate {
           //do my UI updating
        };
        Dispatcher.Invoke(x, new object[] { });

这篇关于并行编程:无法访问UI并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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