Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Matrix Xmpp winforms
Avatar
kingThor #1
Member since Sep 2016 · 4 posts
Group memberships: Members
Show profile · Link to this post
Subject: Please help i am having a cross thread operation
  I am creating a messenger application using c# winforms. loggin in has been okay using openfire server. But where i get a problem is when i try to access a control or change something in a control after the action of an event. its gives me a cross thread operation error please help ...this is some of the code.

ERROR
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on.


CODE
  1. private void loginBtn_Click(object sender, System.EventArgs e)
  2. {
  3.     Util_I.connect(usernameBox.Text, PaswordBox.Text);
  4.  
  5.     #region EVENTS TO FIRE
  6.     Util_I.client.AutoInvoke = true;
  7.     Util_I.client.OnLogin += Client_OnLogin;
  8.     Util_I.client.OnError += Client_OnError;
  9.     Util_I.client.OnAuthError += Client_OnAuthError;
  10.     Util_I.client.OnRosterItem += new EventHandler<RosterEventArgs>(Client_OnRosterItem);
  11.     Util_I.client.OnRosterEnd += Client_OnRosterEnd;
  12.     Util_I.client.OnPresence += Client_OnPresence;
  13.     #endregion
  14. }
  15.  
  16. private void Client_OnPresence(object sender, Matrix.Xmpp.Client.PresenceEventArgs e)
  17. {
  18.    
  19. }
  20.  
  21. private void Client_OnRosterEnd(object sender, Matrix.EventArgs e)
  22. {
  23.     PopulateList.Invoke(new Action(PopulateList.PerformClick));
  24.  
  25. }
  26.  
  27. #region EVENTS
  28. private void Client_OnAuthError(object sender, SaslEventArgs e)
  29. {
  30.     MessageBox.Show("Authentication Error");
  31.    
  32. }
  33.  
  34. private void Client_OnRosterItem(object sender, RosterEventArgs e)
  35. {
  36.     Rosterlist.Add(e.RosterItem.Jid);
  37.     listView1.Items.Add(e.RosterItem.Jid.User); // this is where i get an error accessing the listview
  38. }
  39.  
  40. private void Client_OnError(object sender, ExceptionEventArgs e)
  41. {
  42.     MessageBox.Show("Login Error");
  43.     ChangeInfoLabel(false);
  44.     HidePanel();
  45. }
  46.  
  47. private void Client_OnLogin(object sender, Matrix.EventArgs e)
  48. {
  49.     MessageBox.Show("Login Successful!");
  50.  
  51.     ChangeInfoLabel(true);
  52.     HidePanel();
  53. }
does the event run on a different Thread? or am i doing something wrong. Please a detailed example would be okay

Thank you.
JohnPaul.
This post was edited 2 times, last on 2016-09-29, 19:01 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by kingThor:
does the event run on a different Thread? or am i doing something wrong. Please a detailed example would be okay

yes it runs in multiple threads.
usually MatriX finds the dispatcher for invoking of your UI automatically and does all the magic for you.
If it does not find your dispatcher automatically you can pass it manual to MatriX, or invoke all the events on your own.
See also the MiniClient example.
Avatar
kingThor #3
Member since Sep 2016 · 4 posts
Group memberships: Members
Show profile · Link to this post
Please how can I pass it manually... Am still learning , don't really know about invoking the events
Avatar
kingThor #4
Member since Sep 2016 · 4 posts
Group memberships: Members
Show profile · Link to this post
Subject: success
Hi, Alex
         i just realised that i was using an old version of the matrix.dll. i have changed the version and am using the current version, everything works fine now.

Thank you
Alex.
Avatar
Alex #5
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Great to hear
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: