小弟写了个多线程的测试,为何最后显示的出来的结果有时是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++;
}
}
}
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++;
}
}
}
计算.BeginInvoke(文本.Text, new AsyncCallback(线程回调), null);
delegate int 申明委托签名(string 传入值);
申明委托签名 计算 = new 申明委托签名(线程执行);
public static int 线程执行(string 文本)
{
int 车 = 0;
if (文本.IndexOf("\") >= 0)
{ for (int 程 = 0; 程 < 文本.Length; 程 = 程 + 1 + 文本.IndexOf("\")) { 车++; } }
return 车;
}
public void 线程回调(IAsyncResult 返回值)
{ int 啊 = 计算.EndInvoke(返回值); }
*/
^_^就用中文写来专门恶傻瓜心的