系统.UnauthorizedAccessException -无效的跨线程访问

本文关键字:线程 访问 无效 UnauthorizedAccessException 系统 | 更新日期: 2023-09-27 18:11:31

我有一个非常简单的应用程序,我有使用数据绑定的问题。我已经成功下载使用了这个例子:http://msdn.microsoft.com/en-us/magazine/hh852595.aspx

有"next"按钮,用于生成新的人并将其加载到应用程序中。

然而,我试图做同样的事情,我得到了以下例外:System.UnauthorizedAccessException - Invalid cross-thread access

我试着对这个做同样的事情:

    public Chat()
    {
        InitializeComponent();
        bindingChat.leftText = "jooooo2";
        ContentPanel.DataContext = bindingChat;
    }   
    private void connect(object sender, XmppConnectedEventArgs target)
    {
        bindingChat = new BindingChat();
        bindingChat.leftText = "Connected";
        ContentPanel.DataContext = bindingChat; //this is where the exception is thrown
    }

文本"jooooo2"按预期工作,但当调用connet方法时,出现上述异常。

在这个正常工作的例子中,他们用下面的代码设置了一个新的人(在点击一个按钮之后):

    private void SetDataContext()
    {
        GeneratePerson();
        ContentPanel.DataContext = _currentPerson;
    }

效果很好。


编辑:

好的,我发现这是因为它是间接调用的:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        xmpp1.OnConnected += new Xmpp.OnConnectedHandler(connect);
        xmpp1.IMServer = "***";
        xmpp1.IMPort = 5222;
        xmpp1.Connect("user1", "heslouser1");
        xmpp1.ChangePresence(1, "I'm here!");
    }

如果我试图用另一个按钮直接改变它,它会像预期的那样工作:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        bindingChat = new BindingChat();
        bindingChat.leftText = "button pressed";
        ContentPanel.DataContext = bindingChat;
    }

系统.UnauthorizedAccessException -无效的跨线程访问

嗯,我在http://www.codeproject.com/Articles/368983/Invoking-through-the-Dispatcher-on-Windows-Phone-a上找到了解决方案

这是一个工作代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using nsoftware.IPWorks;
using myNamespace.ViewModel;
namespace myNamespace
{
    public partial class Chat : PhoneApplicationPage
    {
        private Xmpp xmpp1 = new Xmpp();
        private Xmpp xmpp2 = new Xmpp();
        private BindingChat bindingChat = new BindingChat();
        public Chat()
        {
            InitializeComponent();
            bindingChat.leftText = "jooooo2";
            xmpp1.OnConnected += new Xmpp.OnConnectedHandler(connect);
            ContentPanel.DataContext = bindingChat;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            xmpp1.IMServer = "***";
            xmpp1.IMPort = 5222;
            xmpp1.Connect("user1", "heslouser1");
            xmpp1.ChangePresence(1, "I'm here!");
        }
        private void connect(object sender, XmppConnectedEventArgs target)
        {
            DispatchInvoke(() =>
            {
                bindingChat = new BindingChat();
                bindingChat.leftText = "Connected";
                ContentPanel.DataContext = bindingChat;
            }
        );
        }    
        public void DispatchInvoke(Action a)
        {
#if SILVERLIGHT
            if (Dispatcher == null)
                a();
            else
                Dispatcher.BeginInvoke(a);
#else
    if ((Dispatcher != null) && (!Dispatcher.HasThreadAccess))
    {
        Dispatcher.InvokeAsync(
                    Windows.UI.Core.CoreDispatcherPriority.Normal, 
                    (obj, invokedArgs) => { a(); }, 
                    this, 
                    null
         );
    }
    else
        a();
#endif
        }
    }
}