简体   繁体   中英

System.UnauthorizedAccessException - Invalid cross-thread access

I have very simple application and I have issues with using databinding. I have succesfully downloaded used this example : http://msdn.microsoft.com/en-us/magazine/hh852595.aspx

There is "next" button, which generates new person and load it into application.

However I tried to do the same and I got the following exception : System.UnauthorizedAccessException - Invalid cross-thread access

I tried to do the same with this :

    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
    }

Text "jooooo2" works as expected, but when connet method is called, the exception mentioned appears.

In the example, which is working, they set up new person (after clicking a button) with this code :

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

And it works fine.


Edit :

Ok, I found that it is because it is called indirectly with this :

    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!");
    }

If I tried to change it directly with another button, it works as expected :

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

Well, I found solution on http://www.codeproject.com/Articles/368983/Invoking-through-the-Dispatcher-on-Windows-Phone-a

This is a working code :

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
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM