Possible Duplicate:
“Cross-thread operation not valid” exception on inner controls
I am a beginner in C# and lately I run over a cross thread ex. Now I searched high and low to find an answer to my problem however, none seem to be same as mine so hopefully some could help. Here is mine problem explained.
1) I extend a Microsoft.VisualBasic.PowerPacks.OvalShape to add some more functionality to it so it meets my needs and here is what I extend to the class:
public class MyShape: Microsoft.VisualBasic.PowerPacks.OvalShape
{
int imageNumber;
public MyShape()
{
imageNumber = 0;
this.BackgroundImage = global::_4InARow.Properties.Resources.grey;
}
public int getImageNumber()
{
return imageNumber;
}
public bool setImage(int imgNo)
{
imageNumber = imgNo;
if (imgNo == 0) { this.BackgroundImage = global::_4InARow.Properties.Resources.grey; return true; }
else if (imgNo == 1) { this.BackgroundImage = global::_4InARow.Properties.Resources.blue; return true; }
else if (imgNo == 2) { this.BackgroundImage = global::_4InARow.Properties.Resources.red; return true; }
else { return false; }
}
}
2) In designer class I create several objects fom the class MyShape and later add them to a ShapeContainer, using main thread or UI thread.(highlight ShapeContainer)
3) in the main class called (FourInARow_Server) I add all the objects created in a jagged Array using arrayLoad() method. (class is listed below)
The Exception occurs when I asynchronously access the method changeColor(int x, int y, int color) using the worker thread(not the thread in which the objects were created).
However I can access the objects if for instance I say
if(circles[1][1].getImageNumber() == 0)
{//do something};
(this using worker thread and main thread works OK)
One other thing about the exception is that, when complaining that a worker thread called an an object created in the main thread, it is the ShapeContainer object that throws the exception rather than the MyShape object.
I am now looking for a solution to safely acces the changeColor method to change the color of each object using both threads.
Like I said I did look on-line for an answer but none were like what I am facing and I hope some one could help. Thanks!
public partial class FourInARow_Server : Form
{
private MyShape[][] circles = new MyShape[7][];
private Socket newConnection;
private Thread newThread;
private int player;
private bool flag_MyTurn;
private bool flag_ConnectionAlive;
private bool flag_MoveAllowed;
private bool flag_EventBlocker;
public FourInARow_Server()
{
InitializeComponent();
arrayLoad();
player = 1;
flag_MyTurn = true;
flag_ConnectionAlive = false;
flag_MoveAllowed = false;//variable needed to prevent user from loosing its turn if it clicks on a colomn that hasn,t got any more available moves.
flag_EventBlocker = true;
this.labelSystemMessage.Text = "To begin play, please press \"Launch Server\" \n and wait for opponent to connect ";
}
private void arrayLoad()//Load all ovall shapes into an array
{
MyShape[] colOne = { x1y1, x1y2, x1y3, x1y4, x1y5, x1y6};
MyShape[] colTwo = { x2y1, x2y2, x2y3, x2y4, x2y5, x2y6};
MyShape[] colThree = { x3y1, x3y2, x3y3, x3y4, x3y5, x3y6};
MyShape[] colFour = { x4y1, x4y2, x4y3, x4y4, x4y5, x4y6 };
MyShape[] colFive = { x5y1, x5y2, x5y3, x5y4, x5y5, x5y6 };
MyShape[] colSix = { x6y1, x6y2, x6y3, x6y4, x6y5, x6y6};
MyShape[] colSeven = {x7y1, x7y2, x7y3, x7y4, x7y5, x7y6 };
circles[0] = colOne; circles[1] = colTwo; circles[2] = colThree; circles[3] = colFour; circles[4] = colFive; circles[5] = colSix; circles[6] = colSeven;
}
private void changeColor(int x, int y, int color)
{
if (color == 0) { circles[x][y].setImage(0); }
else if (color == 1) { circles[x][y].setImage(1); }
else if (color == 2) { circles[x][y].setImage(2); }
Application.DoEvents();
}
}
Use the following in the worker thread: