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
does the event run on a different Thread? or am i doing something wrong. Please a detailed example would be okay
Thank you.
JohnPaul.
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
- private void loginBtn_Click(object sender, System.EventArgs e)
- {
- Util_I.connect(usernameBox.Text, PaswordBox.Text);
- #region EVENTS TO FIRE
- Util_I.client.AutoInvoke = true;
- Util_I.client.OnLogin += Client_OnLogin;
- Util_I.client.OnError += Client_OnError;
- Util_I.client.OnAuthError += Client_OnAuthError;
- Util_I.client.OnRosterEnd += Client_OnRosterEnd;
- Util_I.client.OnPresence += Client_OnPresence;
- #endregion
- }
- private void Client_OnPresence(object sender, Matrix.Xmpp.Client.PresenceEventArgs e)
- {
- }
- private void Client_OnRosterEnd(object sender, Matrix.EventArgs e)
- {
- }
- #region EVENTS
- private void Client_OnAuthError(object sender, SaslEventArgs e)
- {
- MessageBox.Show("Authentication Error");
- }
- private void Client_OnRosterItem(object sender, RosterEventArgs e)
- {
- Rosterlist.Add(e.RosterItem.Jid);
- listView1.Items.Add(e.RosterItem.Jid.User); // this is where i get an error accessing the listview
- }
- private void Client_OnError(object sender, ExceptionEventArgs e)
- {
- MessageBox.Show("Login Error");
- ChangeInfoLabel(false);
- HidePanel();
- }
- private void Client_OnLogin(object sender, Matrix.EventArgs e)
- {
- MessageBox.Show("Login Successful!");
- ChangeInfoLabel(true);
- HidePanel();
- }
Thank you.
JohnPaul.
This post was edited 2 times, last on 2016-09-29, 20:01 by Alex.