What i'm trying to do is to get a pixel color at a certain position on the current form. But, the point at which I call the method is in a seperate thread. When I run the application, I get an error:
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
Thread Code:
Thread drawThread;
drawThread = new Thread(drawBikes);
drawBikes Code:
public void drawBikes()
{
MessageBox.Show("Bike "+bike.color.ToString()+": "+Form1.ActiveForm.GetPixelColor(bike.location.X, bike.location.Y).ToString());
}
Here is the GetPixelColor Method (in a separate static class):
public static class ControlExts
{
public static Color GetPixelColor(this Control c, int x, int y)
{
var screenCoords = c.PointToScreen(new Point(x, y));
return Win32.GetPixelColor(screenCoords.X, screenCoords.Y);
}
}
Where do I call the Invoke?