私がやろうとしているのは、現在のフォームの特定の位置でピクセルカラーを取得することです。しかし、私がメソッドを呼び出すポイントは、別のスレッドにあります。アプリケーションを実行すると、次のエラーが発生します。
クロススレッド操作が無効です:コントロール'Form1'は、それが作成されたスレッド以外のスレッドからアクセスされました。
スレッドコード:
Thread drawThread;
drawThread = new Thread(drawBikes);
drawBikesコード:
public void drawBikes()
{
MessageBox.Show("Bike "+bike.color.ToString()+": "+Form1.ActiveForm.GetPixelColor(bike.location.X, bike.location.Y).ToString());
}
GetPixelColorメソッド(別の静的クラス)は次のとおりです。
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);
}
}
どこで呼び出しを呼び出しますか?