//为什么textbox1上不显示内容,一定要等到执行完异步代码才在textbox1中显示,这段代码不是阻塞,怎么和阻塞的执行顺序一样using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public delegate DateTime AsyncExampleDelegate(int delay,string name);
public partial class but_Blocking : Form
{
public but_Blocking()
{
InitializeComponent();
}
public static DateTime LongRunningMethod(int delay, string name)
{
but_Blocking myForm = new but_Blocking();
myForm.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + name + "实例线程开始" + "\r\n";
Console.WriteLine(myForm.textBox1.Text.ToString());
Thread.Sleep(delay);
myForm.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + name + "实例线程开始" + "\r\n";
Console.WriteLine(myForm.textBox1.Text.ToString());
//myForm.Show();
return DateTime.Now; } private void but_Blocking_Click(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
{
this.textBox1.Text = "";
}
this.textBox1.Text += Environment.NewLine + "***进行等待实例***" + "\r\n"; AsyncExampleDelegate longRunningMethod = new AsyncExampleDelegate(LongRunningMethod);
IAsyncResult asyncResult = longRunningMethod.BeginInvoke(10000, "Waiting", null, null);
this.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + "等待直到方法结束" + "\r\n"; while (!asyncResult.AsyncWaitHandle.WaitOne(200,false))
{
string aa=DateTime.Now.ToString("HH:mm:ss:ffff") + "等待超时" + "\r\n";
this.textBox1.Text += aa; //为什么textbox1上不显示内容,一定要等到执行完异步代码才在textbox1中显示
Console.WriteLine(aa);
} DateTime completion = longRunningMethod.EndInvoke(asyncResult);
this.textBox1.Text += completion.ToString("HH:mm:ss:ffff") + "等待实例结果" + "\r\n";
this.textBox1.Text += completion.ToString(); }
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public delegate DateTime AsyncExampleDelegate(int delay,string name);
public partial class but_Blocking : Form
{
public but_Blocking()
{
InitializeComponent();
}
public static DateTime LongRunningMethod(int delay, string name)
{
but_Blocking myForm = new but_Blocking();
myForm.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + name + "实例线程开始" + "\r\n";
Console.WriteLine(myForm.textBox1.Text.ToString());
Thread.Sleep(delay);
myForm.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + name + "实例线程开始" + "\r\n";
Console.WriteLine(myForm.textBox1.Text.ToString());
//myForm.Show();
return DateTime.Now; } private void but_Blocking_Click(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
{
this.textBox1.Text = "";
}
this.textBox1.Text += Environment.NewLine + "***进行等待实例***" + "\r\n"; AsyncExampleDelegate longRunningMethod = new AsyncExampleDelegate(LongRunningMethod);
IAsyncResult asyncResult = longRunningMethod.BeginInvoke(10000, "Waiting", null, null);
this.textBox1.Text += DateTime.Now.ToString("HH:mm:ss:ffff") + "等待直到方法结束" + "\r\n"; while (!asyncResult.AsyncWaitHandle.WaitOne(200,false))
{
string aa=DateTime.Now.ToString("HH:mm:ss:ffff") + "等待超时" + "\r\n";
this.textBox1.Text += aa; //为什么textbox1上不显示内容,一定要等到执行完异步代码才在textbox1中显示
Console.WriteLine(aa);
} DateTime completion = longRunningMethod.EndInvoke(asyncResult);
this.textBox1.Text += completion.ToString("HH:mm:ss:ffff") + "等待实例结果" + "\r\n";
this.textBox1.Text += completion.ToString(); }
}
}
或许你对使用回调函数的结果比较满意的.