关于WP多线程的疑惑
小弟写了个多线程的测试,为何最后显示的出来的结果有时是0,有时是50呢,愿各位大侠不吝赐教啊,代码如下:
public partial class MainPage : PhoneApplicationPage
{
int num = 0;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Thread th1 = new Thread(new ThreadStart(GetNum));
Thread th2 = new Thread(new ThreadStart(GetNum));
th1.Start();
th2.Start();
textBlock1.Text = num.ToString();
}
private void GetNum()
{
for (int i = 0; i < 50; i++)
{
num++;
}
}
}