wpf,新建异步方法:
Thread newThread = new Thread(new ParameterizedThreadStart(GetResult)); newThread.Start(inputNumber); private void GetResult(object inputNumber) { //错误的方法 //this.Dispatcher.BeginInvoke((Action)delegate() // { // this.textBox2.Text = CalcSum((Int64)inputNumber).ToString(); //}); //正确的方法 double result=CalcSum((Int64)inputNumber); this.Dispatcher.BeginInvoke((Action)delegate() { this.textBox2.Text = result.ToString(); }); } private double CalcSum(Int64 inputNumber) { double sum=0; for (int i = 0; i < inputNumber; i++) { sum +=i; } return sum; }
this.Dispatcher.Invoke(new Action(delegate() { tbMessage.text = "呵呵呵"}));
获取当前调度线程中的主线程;
注意事项,不要把异步的方法体下载主线程中;