Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
mikees #1
Member since May 2009 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: autoconnect with matrix
Hello,

I would like to connect a user automatically when the application started but I have a exception "System.UnauthorizedAccessException: Invalid cross-thread access error"

I use silverlight client sample.

Roster.xaml.cs

public Roster(XmppClient client)
        {
            InitializeComponent();

            _xmppClient = client;
            _presManager = new PresenceManager(_xmppClient);

            _xmppClient.OnRosterStart += new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterStart);
            _xmppClient.OnRosterItem += new EventHandler<RosterEventArgs>(_xmppClient_OnRosterItem);
            _xmppClient.OnRosterEnd += new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterEnd);
            _xmppClient.OnClose += new EventHandler<Matrix.EventArgs>(_xmppClient_OnClose);
            _xmppClient.OnMessage += new EventHandler<MessageEventArgs>(_xmppClient_OnMessage);
            _xmppClient.OnAuthError += new EventHandler<Matrix.Xmpp.Sasl.SaslEventArgs>(_xmppClient_OnAuthError);

            _xmppClient.OnLogin += new EventHandler<Matrix.EventArgs>(_xmppClient_OnLogin);
            _xmppClient.OnError +=new EventHandler<ExceptionEventArgs>(_xmppClient_OnError);
            _presManager.OnAvailablePresence += new EventHandler<PresenceEventArgs>(presManager_OnAvailablePresence);
            _presManager.OnUnavailablePresence += new EventHandler<PresenceEventArgs>(presManager_OnUnavailablePresence);
       
            [color=crimson]Connect();[/color]
           
        }
private void Connect()
        {
            /* TEST AUTOCONNECT */

            login.txtJid.Text = "MyUser";
            login.txtPassword.Password = "MyPass";
            
            _xmppClient.Username = login.Jid.User;
            _xmppClient.Password = login.Password;
         
            _xmppClient.XmppDomain = login.Jid.Server;
           
            _xmppClient.Port = 4502;

            _xmppClient.AutoPresence = false;
            _xmppClient.Priority = 10;
           
            // status and show for Autopresence after login
            _xmppClient.Status  = "online";
            _xmppClient.Show    = Matrix.Xmpp.Show.NONE;

            _xmppClient.Open();
        }

Thanks.
Avatar
Alex #2
Member since Feb 2003 · 4297 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
try to set a Dispatcher before you connect.

Alex
Avatar
mikees #3
Member since May 2009 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thanks but where I can use Dispatcher ? I have never use Dispatcher so You can give me an example please ?
Avatar
Alex #4
Member since Feb 2003 · 4297 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
for thread safety all events from MatriX must be invoked.
If you set the Dispatcher property to your UI then MatriX does this automatically for you. Otherwise you have to invoke all events on your own.
Normally MatriX does find your UI and the Dispatcher automatically, but this does not work in your case, because you connect before the UI is completely initialized. So set a Dispatcher manual, or connect after the Ui is fully initialized.

Alex
Avatar
mikees #5
Member since May 2009 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thank you very much !

I set a dispatcher manual.

public Roster(XmppClient client)
        {
            InitializeComponent();

            //ShowLogin();

            _xmppClient = client;
            _presManager = new PresenceManager(_xmppClient);

            _xmppClient.OnRosterStart += new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterStart);
            _xmppClient.OnRosterItem += new EventHandler<RosterEventArgs>(_xmppClient_OnRosterItem);
            _xmppClient.OnRosterEnd += new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterEnd);
            _xmppClient.OnClose += new EventHandler<Matrix.EventArgs>(_xmppClient_OnClose);
            _xmppClient.OnMessage += new EventHandler<MessageEventArgs>(_xmppClient_OnMessage);
            _xmppClient.OnAuthError += new EventHandler<Matrix.Xmpp.Sasl.SaslEventArgs>(_xmppClient_OnAuthError);

            _xmppClient.OnLogin += new EventHandler<Matrix.EventArgs>(_xmppClient_OnLogin);
            _xmppClient.OnError +=new EventHandler<ExceptionEventArgs>(_xmppClient_OnError);
            _presManager.OnAvailablePresence += new EventHandler<PresenceEventArgs>(presManager_OnAvailablePresence);
            _presManager.OnUnavailablePresence += new EventHandler<PresenceEventArgs>(presManager_OnUnavailablePresence);


             _xmppClient.Dispatcher = this.Dispatcher;
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterStart));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<RosterEventArgs>(_xmppClient_OnRosterItem));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<Matrix.EventArgs>(_xmppClient_OnRosterEnd));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<Matrix.EventArgs>(_xmppClient_OnClose));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<MessageEventArgs>(_xmppClient_OnMessage));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<Matrix.EventArgs>(_xmppClient_OnLogin));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<ExceptionEventArgs>(_xmppClient_OnError));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<PresenceEventArgs>(presManager_OnAvailablePresence));
             _xmppClient.Dispatcher.BeginInvoke(new EventHandler<PresenceEventArgs>(presManager_OnUnavailablePresence));
             Connect();
        }

and it's ok !

thanks
Avatar
Alex #6
Member since Feb 2003 · 4297 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
adding the Dispatcher should be enough. Then MatriX does all the invoke magic for you.

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: MatriX RSS