我试图编写一个小游戏,你必须跳过几个障碍,然后继续等等。现在,为此我可以移动障碍并同时跳跃,我开了一个新线程。这是在Form1.cs中:
public partial class Form1 : Form
{
Worker workerObject;
Thread workerThread;
public Form1()
{
InitializeComponent();
workerObject = new Worker();
workerObject.Form1 = this;
workerObject.initialize(this);
workerThread = new Thread(workerObject.DoWork);
}
和
public class Worker
{
private volatile bool _shouldStop = false;
public volatile Form Form1;
public void initialize(Form pForm)
{
Form1 = pForm;
}
public void DoWork()
{
while (!_shouldStop)
{
_shouldStop = true;
picture_barrier1.Left = picture_barrier1.Left - 1;
}
}
public void RequestStop()
{
_shouldStop = true;
}
}
但是我收到以下错误:
错误1无法访问外部类型的非静态成员' Game.Form1' 通过嵌套类型' Game.Form1.Worker'
然后,当我进入Form1.Designer.cs并使屏障变为静态时(顺便说一下,对象是易失性的),我从上面的代码中得到11个错误,包含:
错误1会员' Game.Form1.picture_barrier1'无法访问 实例引用;使用类型名称来限定它
如何从另一个线程正确调用这些对象?