C#如何在工作线程中调用主线程创建的对象上的方法?

时间:2011-11-19 10:49:39

标签: c# asynchronous multithreading

  

可能重复:
  “Cross-thread operation not valid” exception on inner controls

我是C#的初学者,最近我遇到了一个跨线程ex。现在我搜索了我的问题的答案,然而,似乎没有一个与我的相同,所以希望有些人可以提供帮助。这是我的问题解释。

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)在设计器类中,我使用class MyShape创建了几个对象,然后使用main threadUI thread.(highlight ShapeContainer)

将它们添加到ShapeContainer中

3)在名为(FourInARow_Server)的主类中,我使用arrayLoad()方法添加在锯齿状数组中创建的所有对象。 (课程如下)

当我使用工作线程(而不是创建对象的线程)异步访问方法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();
    }
}

1 个答案:

答案 0 :(得分:1)

在工作线程中使用以下内容:

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