Help writing an instant messenger app using SQL azure


hello in dire need here. have gotten deep trying figure out how write im app using sql azure. had success writing using background worker instead of threads wanted learn how explicitly use threads , tried threaded version. result= big mess. cant seem figure out own code. have 8 months programming experience maybe 1 of more experience software engineers can me out im sure second nature guys. having 2 problems explain in detailed notes in source code. basically, getting unexpected results when comes updating buddy list. have temp buddy list , real buddylist compared against eachother see has signed on , signed off. real buddy list gets set on form load , temp gets set every second on timer/second thread. weird somehow original buddy list being updated while temp 1 should being set. dont know how happening. other problem events dont seem firing. having difficult time trying work.

 

 

this buddylist window form code:

 

  using system;  using system.collections.generic;  using system.componentmodel;  using system.data;  using system.drawing;  using system.text;  using system.windows.forms;  using telerik.wincontrols;  using system.data.sqlclient;  using system.threading;    namespace crime_information_system  {               public partial class frmmessenger : telerik.wincontrols.ui.radform      {          logon.logincredentials logon;          sqlconnection conn;          list<string> lstusers = new list<string>();          list<long> lstuids = new list<long>();          list<buddy> onlinebuddies = new list<buddy>();          list<buddy> temponlinebuddies = new list<buddy>();          static readonly object locker = new object();          delegate void delegateaddbuddy();          public event onbuddychanged buddychanged;          public delegate void onbuddychanged(object sender, buddyadded e);                            public frmmessenger(logon.logincredentials log)          {              initializecomponent();              logon = log;              conn = connectionstring.getconnection();                        }            private void frmmessenger_load(object sender, eventargs e)          {              buddypool pool = new buddypool();                //sets struct in class equal struct of logged in user              //this struct used in select query uses id struct in               //where clause              pool.setlogoncredentials(logon);              //any user under table users has logon status of 1 (indicates online)              onlinebuddies = pool.retrieveonlineusers();              //starts timer launches check every second update buddy list who              //has signed off , has signed on              timer1.interval = 1000;              timer1.start();                            //just started trying use events not sure if working, im pretty sure isnt.              buddychanged += new onbuddychanged(onbuddyadded);              //buddypool.buddylistchanged += new itemchangedeventhandler(buddypool_buddylistchanged);                        }                        int intcount2 = 0;            private void frmmessenger_formclosing(object sender, formclosingeventargs e)          {              messengerclass.signout(logon.intuserid);              timer1.stop();          }                                  private void onbuddyadded(object sender, buddyadded e)          {              //this method supposed fire when event fires isnt working,              //for reason.              messagebox.show(e.b.username.tostring());          }            private void timer1_tick(object sender, eventargs e)          {              //creates new thread every second update buddy list              //calls method below              thread t = new thread(getusers);              t.isbackground = true;              t.start();              t.join();            }            private void getusers()          {              //creates new instance of buddypool class , sets struct in formload              //event select query can have proper clause              buddypool pool = new buddypool();              pool.setlogoncredentials(logon);              //this fills temporary buddy list supposed checked against the              //actual buddy list see code below. if there mismatch between 2 the              //original buddy list updated.              //the problem occurs here , cant figure out why... there never mismatch              //because somehow original onlinebuddies correctly reflects               //temponlinebuddies has. never differ , should whole              //reason why have code below. somehow onlinebuddies being correctly              //updated before can update code below. stumping me.              temponlinebuddies = pool.retrieveonlineusers();                foreach (buddy b in onlinebuddies)              {//if there no buddy b in temp list exists in buddy list then                  //that means buddy has signed off                  if (!temponlinebuddies.contains(b))                  {                      onlinebuddies.remove(b);                      buddyadded ba = new buddyadded();                      ba.b = b;                      buddychanged(b, ba);                  }                }                foreach (buddy b in temponlinebuddies)              {//if there no buddy b in buddylist in tempbuddy list means                  //the user has signed on                  if (!onlinebuddies.contains(b))                  {                      onlinebuddies.add(b);                      buddyadded ba = new buddyadded();                      ba.b = b;                      buddychanged(b, ba);                  }                  }                           //delegateaddbuddy checkusers = new delegateaddbuddy(pool.retrieveonlineusers);              //this.invoke(checkusers);          }            private void lstbuddylist_itemmousedoubleclick(object sender, telerik.wincontrols.ui.listviewitemeventargs e)          {              //this used open im window when click on username in physical              //listview on instant messenger window.              bool bolsuccess = true;              string strusername = lstbuddylist.currentitem.text;              int = 0;              try              {                  = lstusers.indexof(strusername);              }                catch (exception e)              {                  messagebox.show(e.tostring());                  bolsuccess = false;              }                if (bolsuccess == true)              {                  frmimwindow im = new frmimwindow(strusername, lstuids[i]);                  im.show();              }                          }                    }        public class buddyadded : eventargs      {          public buddy b { get; set; }          }  }    


buddypool class code

  using system;  using system.collections.generic;  using system.linq;  using system.text;  using system.data.sqlclient;  using system.windows.forms;    namespace crime_information_system  {      class buddypool      {          logon.logincredentials logon;          //setting connection string class          sqlconnection conn = connectionstring.getconnection();          list<string> lstusers = new list<string>();          list<long> lstuids = new list<long>();          frmimwindow.messagedetails md;          list<long> imids = new list<long>();          list<long> userids = new list<long>();          list<string> usernames = new list<string>();          list<string> messages = new list<string>();          readonly object locker = new object();          static list<buddy> objbuddylist = new list<buddy>();                                public void setlogoncredentials(logon.logincredentials log)          {              logon = log;          }            public list<buddy> retrieveonlineusers()          {                lock (lstusers)              {                  //lstusers.clear();                  //lstuids.clear();                  //clears buddy list because somereason if dont adds duplicates                  //of buddy online.                  objbuddylist.clear();                    int intloginstatus = 1;                      string qrygetonlineusers = "select username, userid tblusers loggedin = "                      + intloginstatus + " , villageid = " + logon.intvillageid + " , userid != "                      + logon.intuserid; //this logon struct i've been passing methods                  //which has current user's log in info , used build specific queries                    sqlcommand loadup = new sqlcommand(qrygetonlineusers, conn);                    conn.open();                  sqldatareader reader;                  reader = loadup.executereader();                    int = 0;                    try                  {                        while (reader.read())                      {                          buddy mybuddy = new buddy();                          //this seems impractical know swamped thing can not                          //figure out way add buddy objects , eliminate middle                          //man aka lists. use these lists add new entries.                          if (!lstusers.contains(reader.getstring(i)))                          {                              lstusers.add(reader.getstring(i));                              mybuddy.username = (reader.getstring(i));                          }                          i++;                          if (!lstuids.contains(reader.getint64(i)))                          {                              lstuids.add(reader.getint64(i));                              mybuddy.userid = (reader.getint64(i));                          }                          = 0;                          if (mybuddy.userid != null & mybuddy.username != null)                          {                              //this adds actual buddy object list                              objbuddylist.add(mybuddy);                                                        }                      }                        if (reader.hasrows)                      {                          //do nothing                      }                      else                      {                          lstusers.clear();                          lstuids.clear();                      }                    }                    catch (sqlexception t)                  {                      messagebox.show(t.tostring());                  }                    conn.close();              }              //returns buddylist. on form load event list loaded onlinebuddies list              //but on tick event loaded temponlinebuddies list. dont              //understand how hell onlinebuddieslist being correctly updated after              //my form load event when tick event updating temponlinebuddies list?              //i know code mess. trying tackle threading, classes, and              //events in one. know classes , know how work haven't              //had practice them. im trying bust out of procedural shell.              //any advice appreciated. guys! :)              return objbuddylist;          }            public void queallmesseges(logon.logincredentials logon)          {                string qrygetallmesseges = "select imid, userid, messege, username tblinstantmesseges"                  + " userid = " + logon.intuserid;              sqlcommand getmesseges = new sqlcommand(qrygetallmesseges, conn);              conn.open();              sqldatareader reader;              reader = getmesseges.executereader();                  try              {                  while (reader.read())                  {                      if (imids.count == 0) //this occurs first time when list empty                      {                          int = 0;                          imids.add(reader.getint32(i));                          i++;                          userids.add(reader.getint64(i));                          i++;                          messages.add(reader.getstring(i));                          i++;                          usernames.add(reader.getstring(i));                        }                        else                      {                          int = 0;                          int32 imidtest = 0;                          bool bolunique = false;                            imidtest = reader.getint32(i);                            if (!imids.contains(imidtest))                          {                              bolunique = true;                              imids.add(imidtest);                              i++;                              userids.add(reader.getint64(i));                              i++;                              messages.add(reader.getstring(i));                              i++;                              usernames.add(reader.getstring(i));                          }                            else                          {                          }                            }                  }              }                catch (exception t)              {                  messagebox.show(t.tostring());              }                conn.close();                        }          }  }    


hi marv,

welcome msdn forum!

i'm moving thread visual c# general sql azure forum better support.

thank understanding , have nice day!

yoyo.


yoyo jiang[msft]
msdn community support | feedback us


Microsoft Azure  >  Azure SQL Database



Comments

Popular posts from this blog

Conditional formatting a graph vertical axis in SSRS 2012 charts

Register with Power BI failed

SQL server replication error Cannot find the dbo or user defined function........