abcfantasy
Programmer
I don't know why I'm still getting this error, even after using delegates and the invoke method.
This is what I have:
A form is first displayed, where you enter some settings like IP and so on. When you click the button 'Join' I have the following code:
It will create another form passing the settings (name and IP) as parameters. When the other form is created, it will connect to the server and wait for data.
When data is receieved, it passes the string data to the following method which will output on a textbox.
When I run the program and click join, the debugger halts on the line "game.ShowDialog()" with the following error: Cross-thread operation not valid. Control 'txtLog' accessed from a thread other than the thread it was created on.
I tried to use game.Show() instead of ShowDialog(), and somehow, the first client that connects would work perfectly, but the same error occurs for the second client.
Any reasons why this is happening? And any possible work arounds?
(Note: The code was changed and simplified from the original, so tell me if more detail is required)
Thanks in advance
Andrew
ABC -
This is what I have:
A form is first displayed, where you enter some settings like IP and so on. When you click the button 'Join' I have the following code:
Code:
private void btnJoin_Click( object sender, EventArgs e )
{
formGame game = new formGame( txtName.Text, txtIP.Text );
this.Hide();
game.ShowDialog();
game.Dispose();
this.Show();
}
It will create another form passing the settings (name and IP) as parameters. When the other form is created, it will connect to the server and wait for data.
When data is receieved, it passes the string data to the following method which will output on a textbox.
Code:
private void CommunicationReceived( string text )
{
if ( txtLog.InvokeRequired )
{
CommReceivedDelegate d = new CommReceivedDelegate( CommunicationReceived );
this.Invoke( d, text );
}
else
{
txtLog.Text = text;
}
}
When I run the program and click join, the debugger halts on the line "game.ShowDialog()" with the following error: Cross-thread operation not valid. Control 'txtLog' accessed from a thread other than the thread it was created on.
I tried to use game.Show() instead of ShowDialog(), and somehow, the first client that connects would work perfectly, but the same error occurs for the second client.
Any reasons why this is happening? And any possible work arounds?
(Note: The code was changed and simplified from the original, so tell me if more detail is required)
Thanks in advance
Andrew
ABC -