2011-11-19 47 views
1

可能重複:
「Cross-thread operation not valid」 exception on inner controlsC#如何調用由工作線程的主線程創建的對象的方法?

我是C#初學者,最近我跑過來一橫紗前。現在我搜索了高低,找到了我的問題的答案,但是,沒有一個像我一樣,所以希望有人可以提供幫助。這是解釋我的問題。

1)我伸出Microsoft.VisualBasic.PowerPacks.OvalShape一些更多的功能添加到它,所以它符合我的需要和這裏是我延伸到類:

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)在設計器類我創建了幾個對象的FOM的class MyShape後來他們叫我(FourInARow_Server)添加使用arrayLoad()方法交錯數組創建的所有對象的主要類添加到ShapeContainer,使用main threadUI thread.(highlight ShapeContainer)

3)。 (類在下面列出)

當我使用工作線程(不是創建對象的線程)異步訪問方法changeColor(int x, int y, int color)時,會發生異常。 但是,如果比如我說

if(circles[1][1].getImageNumber() == 0) 
    {//do something}; 

(在使用輔助線程和主線程工作確定)我可以訪問的對象

有關異常的另一件事是,當抱怨叫做工作者線程一個在主線程中創建的對象,它是引發異常的ShapeContainer對象而不是MyShape對象。

我現在正在尋找一種解決方案來安全地訪問changeColor方法,以使用兩個線程來更改每個對象的顏色。
就像我說過的,我確實在網上看到了答案,但沒有一個像我所面對的,我希望有人能幫上忙。謝謝!

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(); 
    } 
} 
+0

這已被要求,並回答了很多次的SO。請參閱此頁面右側的「相關」列。 –

回答

1

使用的工作線程如下:

this.Invoke((MethodInvoker) delegate { 
    changeColor(x, y, color); 
}); 
+0

非常感謝Otiel它幫助我解決了這個問題。 – user9349193413

相關問題