Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
johndela1 #1
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: connection wont stay active after method returns
I have a button that is on a form.  The event for the buttons creates an instance of XmppClientConnection and assigns it to a class variable.

when the method returns the user that  got logged in appears offline

I put a sleep in there and that shows that the connection stays active until the sleep runs out and the method returns.

Is this normal?  I would have thought this code would have logged in the user and kept it active till I called the close() method


private void cbChat_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbChat.Checked)
            {
                xmpp = new XmppClientConnection();
                xmpp.Server = "gmail.com";
                xmpp.ConnectServer = "talk.google.com";
                xmpp.Username = "adaptiveai@gmail.com";
                xmpp.Password = "a2i2april";
                xmpp.OnLogin += new ObjectHandler(onLogin);
                xmpp.OnMessage += new agsXMPP.XmppClientConnection.MessageHandler(onMessage);
                xmpp.Open();
                this.cbChat.Text = "Connecting...";
                Application.DoEvents();
                System.Threading.Thread.Sleep(9000);
            }
            else
            {
                xmpp.Close();
                xmpp = null;
                this.cbChat.Text = "Connect";
            }
        }
private void onLogin(object Sender)
        {
            xmpp.SendMyPresence();
           // xmpp.Send(new agsXMPP.protocol.client.Message("foo@gmail.com",
            //   agsXMPP.protocol.client.MessageType.chat, "Hello, how are you?"));
            this.cbChat.Text = "Disconnect";
        }
        private void onMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            Console.WriteLine(msg.Body);
        }
Avatar
Alex #2
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
no thats not normal. The connection will be alive until you call the close or the server is closing the session.
Your code looks OK and should work without the DoEvents and sleep.
The Open method is async and starts a new thread. The library shouldn't care about the sleep. This should only suspend your GUI thread.
Did you try to fetch the debug XML Log?
I hope the password in the code is not your real password.

Alex
Avatar
johndela1 #3
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: fixed
I have my main class that is used in Application.run
it makes another form (that is where the code I posed was)

I moved that code to the main class and my event now calls a method in the main class and it all works

If you want or need to know more about this issue let me know...

thanks for the quick reply
Avatar
johndela1 #4
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: now the really strange part
ok...

If you still follow,

In my working code I simply but a line that sets a text attrib of a textbox to say  "disconnect"

that one line breaks everything...It wont stay connected.

 private void onLogin(object Sender)
        {
            xmpp.SendMyPresence();
            this.pcProbe.cbChat.Text = "Disconnect"; // breaks it...
        }
Avatar
johndela1 #5
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: cross thread op
I see the issue...

this spells it out:

System.InvalidOperationException: Cross-thread operation not valid: Control 'cbChat' accessed from a thread other than the thread it was created on.
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.set_WindowText(String value)
   at System.Windows.Forms.Control.set_Text(String value)
   at System.Windows.Forms.ButtonBase.set_Text(String value)

the event handler is in a different thread...


bummer
Avatar
Alex #6
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello,

your issue is related to multithreading. Most Windows.Forms controls are not thread safe. The library is using threading and runs in another thread than your Main GUI. All Events you get from the lib belong to another thread than your GUI. When you try to update your GUI from an event, then your application may hang. Some controls are thread safe and some are not. The best way is to invoke in all your events which perform GUI calls. In the MiniClient you see how the invoke works. Please take a look at the MiniClient.
The invoke brings your app back to the main GUI thread, and will let you update the GUI without problems.

Alex
Avatar
johndela1 #7
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: mini has same issue on visual stuidio 2005
I'm using visual studio 2005 or ver 8. 

in the mini client it crashes on my machine inthis method
private void XmppCon_OnClose(object sender)
        {
            Console.WriteLine("OnClose");
            mnuFileConnect.Enabled        = true;
            mnuFileDisconnect.Enabled    = false;

    here -->    cboStatus.Text = "offline";
            InitRosterListView();
        }
       
the error message says I can turn off this option and ignore this issue
We just upgraded to ver8 vis stud 7 seemed to be more lax by default
Avatar
Alex #8
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
This handler has no invoke yet. Look at the other event handlers. Presence or Message. They have the invoke code.
In the next release version i will update this and everything will be updated to VS2005.

Alex
Avatar
johndela1 #9
Member since Nov 2005 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: learned a lot
I've learned a lot here...
This is all new to me.
here is my current fix (I think I need to refine it):

delegate void SetTextCallback(string text);

        private void setT(string s)
        {
            this.pcProbe.cbChat.Text = s;
        }
     
        private void onLogin(object Sender)
        {
            xmpp.SendMyPresence();
        
            SetTextCallback d = new SetTextCallback(setT);
            this.Invoke(d, new object[] { "Disconnect" });
        }
Avatar
Alex #10
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
hi johndela1,

this is the code from my current MiniClient.
I do it this way:


    private void XmppCon_OnClose(object sender)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new ObjectHandler(XmppCon_OnClose), new object[] { sender });
                return;
            }
            Console.WriteLine("OnClose");
            connectToolStripMenuItem.Enabled    = true;
            disconnectToolStripMenuItem.Enabled    = false;

            cboStatus.Text = "offline";
            InitRosterListView();
        }

        private void XmppCon_OnLogin(object sender)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new ObjectHandler(XmppCon_OnLogin), new object[] { sender });
                return;
            }

            connectToolStripMenuItem.Enabled        = false;
            disconnectToolStripMenuItem.Enabled        = true;           
        }
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: agsXMPP RSS