现在,它输出 00,而不是 54。我正在学习线程,我卡在这里,我不知道现在该怎么办
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Point[] array = new Point[20];
public Form1()
{
for (int i = 0; i < 20; i++)
{
array[i] = new Point(); // I'm creating objects here
}
InitializeComponent();
}
void function1()
{
array[0].x = 5;
array[0].y = 4;
}
void function2()
{
label1.Text = array[0].ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread1 = new Thread(function1);
thread1.Start();
Thread.Sleep(500);
function2();
}
}
class Point
{
public int x;
public int y;
public override string ToString()
{
return x.ToString() + y.ToString();
}
}
}
当我这样做时(没有另一个线程)
private void Form1_Load(object sender, EventArgs e)
{
function1();
function2();
}
它工作正常,输出为 54 谢谢