<P>if (this.lblInfo.InvokeRequired)<BR>this.Invoke(new Action(() =><BR>{<BR>this.lblInfo.Text = "服务器连接失败,请稍后再试";<BR>Thread.Sleep(10000);<BR>this.lblInfo.Text = "";<BR>}));<BR>else<BR>{<BR>this.lblInfo.Text = "服务器连接失败,请稍后再试";<BR>Thread.Sleep(10000);<BR>this.lblInfo.Text = "";<BR>}</P>
<P mce_keep="true"> </P>
<P>这段代码里的this.lblInfo.InvokeRequired是否必须,不是说this.Invoke什么情况下都能用吗?</P>
<P>另外这段代码是想要实现一个文本显示10s然后消失的效果,代码有没有改进的空间,对于线程操作界面这块刚接触请指点</P>
问题补充:
刚测试了一下,代码肯定是有问题,呵呵,
后来我将代码改成了这样,可以运行,求代码的优化和挑错
_SetLblInfo("服务器连接失败,请稍后再试");
ThreadPool.RegisterWaitForSingleObject(new AutoResetEvent(false),
new WaitOrTimerCallback(delegate(object o, bool b)
{
this._SetLblInfo("");
}), null, 10000, true);
private void _SetLblInfo(string msg)
{
if (this.lblInfo.InvokeRequired)
this.Invoke(new Action(() =>
{
this.lblInfo.Text = msg;
}));
else
this.lblInfo.Text = msg;
}