怎么在线程结束的时候给textbox赋值,我的界面是这样的。是多个线程,这里我只写了一个。线程指向的方法写在一个类里面。
if (!thread.IsAlive)
{
this.Invoke((EventHandler)(delegate//timer控件启动线程
{
textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}));
}
if (!thread.IsAlive)
{
this.Invoke((EventHandler)(delegate//timer控件启动线程
{
textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}));
}
完成后有个事件RunWorkerCompleted 。这把代码写这就行了。
一是把 TextBox 传到线程中去,然后用 TextBox.Invoke 显示时间
二是把没信号的 AutoResetEvent 传到线程中去,然后用 AutoResetEvent.Set() 设为有信号,在主窗体判断是 if(AutoResetEvent.WaitOne(0)) { 显示时间,删除 AutoResetEvent}
public void Data_insert()
{
Thread tr = null;
Thread swr = null;
Thread srr = null;
Thread str = null;
Thread spr = null;
Thread fs= null;
Thread fl = null;
Thread fx= null;
string[] type = { "ST_RIVER_R", "ST_WAS_R", "ST_RSVR_R", "ST_TIDE_R", "ST_PPTN_R", "" };
for (int i = 0; i < type.Length; i++)
{
switch (type[i])
{ case "ST_RIVER_R": //河道。
{
try
{
tr = new Thread(new ParameterizedThreadStart(ST_RIVER_R));
tr.Name = "ST_RIVER_R";
tr.IsBackground = true;
tr.Start(listBox1);
}
catch (Exception)
{
throw;
}
break;
}
case "ST_WAS_R": //闸坝。
{
try
{
swr = new Thread(new ParameterizedThreadStart(ST_WAS_R));
swr.IsBackground = true;
swr.Start(listBox1);
}
catch (Exception)
{
throw;
}
break;
}
case "ST_RSVR_R": //水库。
{
try
{
srr = new Thread(new ParameterizedThreadStart(ST_RSVR_R));
srr.IsBackground = true;
srr.Start(listBox1);
}
catch (Exception)
{ throw;
}
break;
}
case "ST_TIDE_R": //潮位。
{
try
{
str = new Thread(new ParameterizedThreadStart(ST_TIDE_R));
str.IsBackground = true;
str.Start(listBox1);
}
catch (Exception)
{ throw;
}
break;
}
case "ST_PPTN_R":
{
try
{
spr = new Thread(new ParameterizedThreadStart(ST_PPTN_R));
spr.IsBackground = true;
spr.Start(listBox1);
}
catch (Exception)
{ throw;
} break;
}
default:
{
try
{
fs = new Thread(new ParameterizedThreadStart(fengSpeed));
fs.IsBackground = true;
fs.Start(listBox1); fl = new Thread(new ParameterizedThreadStart(fengLi));
fl.IsBackground = true;
fl.Start(listBox1); fx = new Thread(new ParameterizedThreadStart(fengXiang));
fx.IsBackground = true;
fx.Start(listBox1);
}
catch (Exception)
{ throw;
}
break;
}
}
}
if ((!tr.IsAlive) && (!swr.IsAlive) && (!srr.IsAlive) && (!str.IsAlive) && (!spr.IsAlive) && (!fs.IsAlive) && (!fl.IsAlive) && (!fx.IsAlive))
{
this.Invoke((EventHandler)(delegate//timer控件启动线程
{
//textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
SetText3("最后同步时间为:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}));
}
//this.BeginInvoke((EventHandler)(delegate { //异步
//}));
}