Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi iam trying an xmpp connection using singleton class,but a methode and a variable(isxmppsuccess) showing errors.My textboxes also throwing error!
how i can maintain the connection in next page also?

i am new to the singleton
below mentioned is my full code and second one is error lines:



my full code:
C#
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 WP8Xmpp.Resources;
using System.Net.XMPP;
using System.Threading;

namespace WP8Xmpp
{
    public partial class MainPage : PhoneApplicationPage
    {

        public MainPage()
        {
            InitializeComponent();

        }




        private static XMPPClient ObjXmppClient;



        public class singleton

        {


            private static singleton instance;

            private static XMPPConnection ObjXmppCon;

             private Boolean IsXmppSuccess { get; set; }

            public String UserName { get; set; }

            public String PassWord { get; set; }

            private readonly String Server = "taurus";


            private readonly String ServerIPAddress = "127.0.0.1:9090";//127.0.0.1:9090/

            private singleton()
            {


            }

            public static singleton GetInstance()
            {


                if (instance == null)
                {

                    instance = new singleton();


                }
                return instance;
            }



            public static void IsXmppValid(string UserName, string PassWord)
            {


                ObjXmppClient = new XMPPClient();
                ObjXmppClient.JID = UserName + "@" + instance.Server;
                ObjXmppClient.Password = PassWord;
                ObjXmppClient.Server = instance.ServerIPAddress;  //user server for emulator and ip address for device.
                ObjXmppClient.AutoReconnect = true;
                ObjXmppClient.RetrieveRoster = true;
                ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
                ObjXmppClient.AutoAcceptPresenceSubscribe = true;
                ObjXmppClient.AttemptReconnectOnBadPing = true;
                ObjXmppCon = new XMPPConnection(ObjXmppClient);
                ObjXmppCon.Connect();
                ObjXmppClient.Connect();
                //initializing the xmpp connection
                ObjXmppCon.OnAsyncConnectFinished +=instance.ObjXmppCon_OnAsyncConnectFinished;
                ObjXmppClient.OnStateChanged +=  new EventHandler(instance.XMPPClient_OnStateChanged);
                Thread.Sleep(2000);


            }



           public  void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
            {


              IsXmppSuccess = client.Connected;


            }



           public   void XMPPClient_OnStateChanged(object sender, EventArgs e)//error
           {
               switch (ObjXmppClient.XMPPState)
               {
                   case XMPPState.Ready:
                       if (IsXmppSucces)//error
                       {
                           this.Dispatcher.BeginInvoke(() =>
                           {
                               //CloseOverLay();

                               //MessageBox.Show("Successfully logged in");
                               NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));//error
                               //return;
                           });
                       }
                       else
                       {

                           this.Dispatcher.BeginInvoke(() =>//error
                           {
                               MessageBox.Show("Check server name/IpAddress");

                               return;
                           });
                       }
                       break;

                   case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>//error
                   {
                       MessageBox.Show("Enter valid username and password");

                       return;

                   }); break;
               }
           }







            private void btnLogin_Click(object sender, RoutedEventArgs e)//error
            {
                if (txtUserName.Text.Trim() == string.Empty)//error
                {
                    MessageBox.Show("Enter Username");
                    return;
                }
                if (txtPassword.Password.Trim() == string.Empty)
                {
                    MessageBox.Show("Enter Password");
                    return;
                }

                UserName = txtUserName.Text.Trim();
                PassWord = txtPassword.Password.Trim();
                IsXmppValid();
            }


        }
    }
}




2> i am getting error in these methodes:

C#
public   void XMPPClient_OnStateChanged(object sender, EventArgs e)//error
           {
               switch (ObjXmppClient.XMPPState)
               {
                   case XMPPState.Ready:
                       if (IsXmppSucces)  // doesnot exist error
                       {
                           this.Dispatcher.BeginInvoke(() =>//<pre>Dispatcher-doesnot contain the definition error
                           {
                               //CloseOverLay();

                               //MessageBox.Show(&quot;Successfully logged in&quot;);
                               NavigationService.Navigate((new Uri(&quot;/Output.xaml?key=success&quot;, UriKind.Relative)));//error
                               //return;
                           });
                       }
                       else
                       {

                           this.Dispatcher.BeginInvoke(() =&gt;//error
                           {
                               MessageBox.Show(&quot;Check server name/IpAddress&quot;);

                               return;
                           });
                       }
                       break;

                   case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =&gt;//error
                   {
                       MessageBox.Show(&quot;Enter valid username and password&quot;);

                       return;

                   }); break;
               }
           }




</pre>





2.
private void btnLogin_Click(object sender, RoutedEventArgs e)//error
{
if (txtUserName.Text.Trim() == string.Empty)//cannot access non static member
{
MessageBox.Show("Enter Username");
return;
}
if (txtPassword.Password.Trim() == string.Empty)
{
MessageBox.Show("Enter Password");
return;
}

UserName = txtUserName.Text.Trim();
PassWord = txtPassword.Password.Trim();
IsXmppValid();//0 arguments error
}
Posted

1 solution

So a couple of things. Your singleton isn't thread safe, and could race with the way it's implemented now. There is an excellent article about it at:
http://csharpindepth.com/Articles/General/Singleton.aspx

The easiest fix is to change the instance property to:
C#
private static Lazy<singleton> instance = new Lazy<singleton>(() => new singleton());
public static singleton GetInstance { get { return instance.Value; } }


Now, the issue that you're seeing is related to passing events to other objects that call a private variable (IsXmppSuccess). Make that variable public.
 
Share this answer
 
Comments
[no name] 21-Oct-15 3:44am    
how can i call this method in to singleton class?
Nathan Minier 21-Oct-15 6:59am    
The same way you would have before, via singleton.GetInstance. In this case GetInstance is a property, so you wouldn't call it like a method (no parentheses), but otherwise the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900