写法3
private void button1_Click(object sender, RoutedEventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(() =>
{
//过程
});
thread1.Start(); }
使用控件时会引起 其他信息: 由于其他线程拥有此对象,因此调用线程无法对其进行访问。
写法2 一般采用这种写法
private void DoWork()
{
for (int i = 0; i < 500; i++)
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
//在这里执行代码
button1.Content = Convert.ToString(i);
}));
System.Threading.Thread.Sleep(10);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread1.Start();
}写法1
private void Button_Click_1(object sender, RoutedEventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
for (int i = 0; i < 500; i++)
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
//your code herer...
button1.Content = Convert.ToString(i);
}));
System.Threading.Thread.Sleep(10);
}
}
));
thread1.Start();
}使用控件时会引起 其他信息: 由于其他线程拥有此对象,因此调用线程无法对其进行访问。