当我提出延迟时,表单上的消息不起作用 [英] message on form doesn't work when I put delay

查看:54
本文介绍了当我提出延迟时,表单上的消息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



按下按钮后我想延迟标签上的信息



/ /System.Threading.Thread.Sleep(5000); < - 这不起作用,但确实如此,但标签上的消息不会在表格上显示



  private   void  button12_Click_1( object  sender,EventArgs e)
{


label1.Text = message;


// System.Threading.Thread.Sleep(5000);

Form1 start = new Form1();
start.Show();
this .Hide();
}

解决方案

最简单的方法是添加以下行:



 私人  void  button12_Click_1(  object  sender,EventArgs e)
{


label1.Text = message;

Application.DoEvents();

System.Threading.Thread.Sleep( 5000 );

Form1 start = new Form1();
start.Show();
this .Hide();
}





但它不是正确的方式。如果你想延迟5秒钟,最好创建一个线程来做到这一点,例如:



 私有  void  button12_Click_1(对象发​​件人,EventArgs e)
{
label1.Text = message;

System.Threading.ThreadPool.QueueUserWorkItem(t = >
{
System.Threading.Thread.Sleep ( 5000 );
Form1 start = new Form1();
start。 Show();
});

}


在UI线程中放入 Sleep 是完全没有意义的。原则上,例如,用于允许某些硬件操作的非常小的延迟不会受到伤害,但即使这样也会表明错误的方法。如果你有任何延迟的动作,无论出于什么原因,它应该在其他一些线程中完成。



但是,如果你只是需要显示一些UI效果延迟相对于用户的动作(我不会这样做;为什么?),可以使用计时器。请参阅:

最容易使用,但如果您使用它,请忘记与准确性相关的任何要求: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29。 aspx [ ^ ];

http://msdn.microsoft.com/en-us/library/system.threading.timer%28v=vs.110%29.aspx [ ^ ],

http: //msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx [ ^ ]。



最后两个上面引用的计时器类需要使用UI线程调用机制,处理程序必须对UI做一些事情,因为那些计时器不能保证在UI线程中调用处理程序。请参阅:

http:// msdn。 microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/a1hetckb(v = vs.110)的.aspx [<一个href =http://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ],

http://msdn.microsoft.com/ en-us / library / 0b1bf3y3(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/a06c0dc2(v=vs.110).aspx [ ^ ]。



并且还看到我过去的答案:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



-SA

Hi everyone,

I want to delay message from label after pushing the button

//System.Threading.Thread.Sleep(5000); <- this doesn't work, it does but message from label doesn't apper on form

private void button12_Click_1(object sender, EventArgs e)
{
    

    label1.Text = "message ";


    //System.Threading.Thread.Sleep(5000);

    Form1 start = new Form1();
    start.Show();
    this.Hide();
}

解决方案

"Easiest" way to do this is to just add the following line:

private void button12_Click_1(object sender, EventArgs e)
{
    
 
    label1.Text = "message ";
 
    Application.DoEvents();

    System.Threading.Thread.Sleep(5000);

    Form1 start = new Form1();
    start.Show();
    this.Hide();
}



But its not the "right" way. If you want to delay something for 5 seconds, its better to create a thread to do that, something like:

private void button12_Click_1(object sender, EventArgs e)
{
    label1.Text = "message ";
 
    System.Threading.ThreadPool.QueueUserWorkItem( t => 
        { 
           System.Threading.Thread.Sleep(5000);
           Form1 start = new Form1();
           start.Show();
        });

}


It's totally pointless to put Sleep in a UI thread. In principle, a really small delay, used, for example, to allow for some hardware action, would not hurt, but even that would be an indication of wrong approach. If you have any action to be delayed, by whatever reason, it should be done in some other thread.

However, if you simply need to show some UI effect delayed relative to the user's action (I would not do it; why?), a timer can be used. Please see:
Easiest to use, but if you use it, forget any requirements related to accuracy: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[^];
http://msdn.microsoft.com/en-us/library/system.threading.timer%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx[^].

The last two timer classes referenced above would need to use UI thread invocation mechanism is the handlers have to do something with UI, because those timers won't guarantee calling your handlers in the UI thread. Please see:
http://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0b1bf3y3(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/a06c0dc2(v=vs.110).aspx[^].

And also see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


这篇关于当我提出延迟时,表单上的消息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆