Grid.children.clear 线程错误 xmpp wpf

Posted

技术标签:

【中文标题】Grid.children.clear 线程错误 xmpp wpf【英文标题】:Grid.children.clear thread error xmpp wpf 【发布时间】:2013-05-31 07:01:45 【问题描述】:

我正在调用具有Grid.Children.Clear() 功能的方法。从不同的方法调用它时效果很好。但是当我从 xmpp_onmessage 方法调用我的方法时。我遇到错误。 “调用线程无法访问此对象,因为不同的线程拥有它。”

这里是包含 Grid.Children.Clear() 的方法:

 private void ConstructChatView(Boolean isChat)
       
        System.Uri resourceUri = new System.Uri("Public/Images/chat_green-textarea.png", UriKind.Relative);
        StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

        System.Uri resourceUri2 = new System.Uri("Public/Images/chat_green-textarea-tail.png", UriKind.Relative);
        StreamResourceInfo streamInfo2 = Application.GetResourceStream(resourceUri2);

        System.Uri resourceUri3 = new System.Uri("Public/Images/chat_blue-textarea.png", UriKind.Relative);
        StreamResourceInfo streamInfo3 = Application.GetResourceStream(resourceUri3);

        System.Uri resourceUri4 = new System.Uri("Public/Images/chat_blue-textarea-tail.png", UriKind.Relative);
        StreamResourceInfo streamInfo4 = Application.GetResourceStream(resourceUri4);


        BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
        var brush = new ImageBrush();
        brush.ImageSource = temp;

        BitmapFrame temp2 = BitmapFrame.Create(streamInfo2.Stream);

        BitmapFrame temp3 = BitmapFrame.Create(streamInfo3.Stream);
        var brush2 = new ImageBrush();
        brush2.ImageSource = temp3;

        BitmapFrame temp4 = BitmapFrame.Create(streamInfo4.Stream);

        int ctr = 0;
        chatGrid.Children.Clear();
        if (isChat == true)
        
            for (int i = 0; i < _messageView.Count; i++)
            

                if ((!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
                
                    RowDefinition chatGridRow1 = new RowDefinition();
                    RowDefinition chatGridRow2 = new RowDefinition();
                    RowDefinition chatGridRow3 = new RowDefinition();

                    chatGrid.RowDefinitions.Add(chatGridRow1);
                    chatGrid.RowDefinitions.Add(chatGridRow2);
                    chatGrid.RowDefinitions.Add(chatGridRow3);

                    if (_messageView.ElementAt(i).IsMe == true)
                    
                        TextBlock Message = new TextBlock();
                        Message.Foreground = Brushes.White;
                        Message.Padding = new Thickness(10, 10, 10, 10);
                        Message.HorizontalAlignment = HorizontalAlignment.Right;
                        Message.Margin = new Thickness(0, 0, 5, 0);
                        Message.Background = brush2;
                        Message.TextWrapping = TextWrapping.Wrap;
                        Message.Text = _messageView.ElementAt(i).Message;
                        Grid.SetRow(Message, ctr);
                        Grid.SetColumn(Message, 0);
                        ctr++;

                        Image Bluetail = new Image();
                        Bluetail.Source = temp4;
                        Bluetail.HorizontalAlignment = HorizontalAlignment.Right;
                        Bluetail.Height = 10;
                        Bluetail.Width = 20;
                        Bluetail.Margin = new Thickness(0, -(0.7), 10, 0);
                        Grid.SetRow(Bluetail, ctr);
                        ctr++;

                        Label Sender = new Label();
                        Sender.Foreground = Brushes.White;
                        Sender.Margin = new Thickness(0, 0, 0, 10);
                        Sender.HorizontalAlignment = HorizontalAlignment.Right;
                        Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                        Grid.SetRow(Sender, ctr);
                        Grid.SetColumn(Sender, 0);
                        ctr++;

                        chatGrid.Children.Add(Message);
                        chatGrid.Children.Add(Bluetail);
                        chatGrid.Children.Add(Sender);
                    
                    else
                    
                        TextBlock Message = new TextBlock();
                        Message.Foreground = Brushes.White;
                        Message.Padding = new Thickness(10, 10, 10, 10);
                        Message.HorizontalAlignment = HorizontalAlignment.Left;
                        Message.Margin = new Thickness(5, 0, 0, 0);
                        Message.Background = brush;
                        Message.TextWrapping = TextWrapping.Wrap;
                        Message.Text = _messageView.ElementAt(i).Message;
                        Grid.SetRow(Message, ctr);
                        Grid.SetColumn(Message, 0);
                        ctr++;

                        Image Greentail = new Image();
                        Greentail.Source = temp2;
                        Greentail.HorizontalAlignment = HorizontalAlignment.Left;
                        Greentail.Height = 10;
                        Greentail.Width = 20;
                        Greentail.Margin = new Thickness(10, -(0.7), 5, 0);
                        Grid.SetRow(Greentail, ctr);
                        ctr++;

                        Label Sender = new Label();
                        Sender.Foreground = Brushes.White;
                        Sender.Margin = new Thickness(0, 0, 0, 10);
                        Sender.HorizontalAlignment = HorizontalAlignment.Left;
                        Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                        Grid.SetRow(Sender, ctr);
                        Grid.SetColumn(Sender, 0);
                        ctr++;

                        chatGrid.Children.Add(Message);
                        chatGrid.Children.Add(Greentail);
                        chatGrid.Children.Add(Sender);
                    
                

            
        
        else
        
            for (int i = 0; i < _messageView.Count; i++)
            

                if (_messageView.ElementAt(i).IsMe == true && (!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
                
                    RowDefinition chatGridRow1 = new RowDefinition();
                    RowDefinition chatGridRow2 = new RowDefinition();
                    RowDefinition chatGridRow3 = new RowDefinition();

                    chatGrid.RowDefinitions.Add(chatGridRow1);
                    chatGrid.RowDefinitions.Add(chatGridRow2);
                    chatGrid.RowDefinitions.Add(chatGridRow3);


                    TextBlock Message = new TextBlock();
                    Message.Foreground = Brushes.White;
                    Message.Margin = new Thickness(0, 10, 300, 0);
                    Message.Padding = new Thickness(10, 10, 10, 10);
                    Message.HorizontalAlignment = HorizontalAlignment.Left;
                    Message.Background = brush;
                    Message.TextWrapping = TextWrapping.Wrap;
                    Message.Text = _messageView.ElementAt(i).Message;
                    Grid.SetRow(Message, ctr);
                    Grid.SetColumn(Message, 0);
                    ctr++;

                    Image Greentail = new Image();
                    Greentail.Source = temp2;
                    Greentail.HorizontalAlignment = HorizontalAlignment.Left;
                    Greentail.Height = 10;
                    Greentail.Width = 20;
                    Greentail.Margin = new Thickness(5, -(0.7), 0, 0);
                    Grid.SetRow(Greentail, ctr);
                    ctr++;

                    Label Sender = new Label();
                    Sender.Foreground = Brushes.White;
                    Sender.Margin = new Thickness(0, 0, 0, 10);
                    Sender.Content = "Sent By : " + _messageView.ElementAt(i).Name.ToString() + " " + _messageView.ElementAt(i).DateCreated.ToString();
                    Grid.SetRow(Sender, ctr);
                    Grid.SetColumn(Sender, 0);
                    ctr++;

                    chatGrid.Children.Add(Message);
                    chatGrid.Children.Add(Greentail);
                    chatGrid.Children.Add(Sender);
                

            
        
        //for (int i = 0; i < _messageView.Count; i++)
        //
        //    if (_messageView.ElementAt(i).IsMe == true && (!_messageView.ElementAt(i).Message.ToString().Trim().Equals("")))
        //    
        //    


        //
        ctr = 0;

        scrollView.ScrollToEnd();
    

有什么想法吗?谢谢

【问题讨论】:

【参考方案1】:

大多数 UI 元素只能在 UI 线程中修改。由于您的事件处理程序显然是在不同的线程中调用的,因此您必须使用 Dispatcher 在 UI 线程中调用您的代码。

private void ConstructChatView(Boolean isChat)

    Dispatcher.Invoke((Action)(() => chatGrid.Children.Clear()));

编辑:您还可以使用 anonymous method 将更多代码传递给 Invoke 调用:

private void ConstructChatView(Boolean isChat)

    Dispatcher.Invoke((Action)(() =>
        
            // more code here
        ));

当然,您也可以将一堆代码放在另一个方法中,然后将其传递给 Invoke 调用:

private void ConstructChatView(Boolean isChat)

    Dispatcher.Invoke((Action)(() => ConstructChatViewInUI(isChat)));


private void ConstructChatViewInUI(Boolean isChat)

    ...

【讨论】:

感谢回复。但我正在尝试用代码创建我的整个网格。那么如何将我的整个代码包装到调用中呢?查看我编辑的帖子。 你也可以考虑使用异步的BeginInvoke来代替Invoke。 你能给我举个例子吗?我无法将我的代码包装到调用中。我试过这个。 Dispatcher.Invoke((Action)(() => //代码放在这里 ));

以上是关于Grid.children.clear 线程错误 xmpp wpf的主要内容,如果未能解决你的问题,请参考以下文章

使用线程的分段错误

Python OpenCV 错误:当前线程不是对象的线程

Google App Engine 错误:获取的线程既不是原始请求线程也不是 ThreadManager 创建的线程

C/多线程/分段错误/(可能是)线程队列问题

Day625.线程池常见错误 -Java业务开发常见错误

Java常见编程错误:线程池