延迟功能一小段时间 [英] delaying a function for a small time

查看:67
本文介绍了延迟功能一小段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按钮点击事件中同时调用两个函数。







 label1.text =   hai; 
JukeBox_Timer.Stop();
JukeBox_Timer.Start();

thread.sleep( 3000 );
clearfstext(pagenation_value);

settheme( \\\\ Page Selection.swf);







这里我要显示标签然后等待三秒然后

settheme(\\\\ Selection Selection.swf);

活动来了



但这里问题是它没有显示label.text线程休眠并直接转到函数settheme(\\\\ Selection Selection.swf);

解决方案

< blockquote> lable1.Text =hai; 不会立即执行。与所有GUI操作一样,它会在下一个UI重绘之前进行排队。但是UI线程被 thread.Sleep(3000); 阻止。



所以你需要执行睡在工人线程中的一部分。类似

  private   void  DelayedMethod()
{
System.Threading.Thread.Sleep( 3000 );

SetTheme( Whatever);
}

void YourCurrentMethodOnUiThread()
{
label1.Text = hai;

System.Threading.Thread otherThread = new System.Threading.Thread(DelayedMethod);
otherThread.Start();
}


一个简单的解决方案是使用Refresh方法强制立即重绘标签。



 label1.text =   hai; 
label1.Refresh();
JukeBox_Timer.Stop();
JukeBox_Timer.Start();

thread.sleep( 3000 );
clearfstext(pagenation_value);

settheme( \\\\ Page Selection.swf);


i'm calling two functions simultaneously in a button click event.



   label1.text="hai";
  JukeBox_Timer.Stop();
  JukeBox_Timer.Start();

thread.sleep(3000);
  clearfstext(pagenation_value);

  settheme("\\Page Selection.swf");




here i want to show the label then it waits for three second then the
settheme("\\Page Selection.swf");
event come

but here is the problem that it does not show the label.text the thread sleeps and directly go to the function settheme("\\Page Selection.swf");

解决方案

lable1.Text = "hai"; doesn't get executed immediately. Like all GUI operations, it gets queued inernally until the next UI redraw. But the UI thread is blocked by thread.Sleep(3000);.

So you need to execute the sleep part in a worker thread. Something like

private void DelayedMethod()
{
    System.Threading.Thread.Sleep(3000);

    SetTheme("Whatever");
}

void YourCurrentMethodOnUiThread()
{
    label1.Text = "hai";

    System.Threading.Thread otherThread = new System.Threading.Thread(DelayedMethod);
    otherThread.Start();
}


A simple solution is to force immediate redraw of the label using the Refresh method.

label1.text="hai";
label1.Refresh();
JukeBox_Timer.Stop();
JukeBox_Timer.Start();

thread.sleep(3000);
clearfstext(pagenation_value);

settheme("\\Page Selection.swf");


这篇关于延迟功能一小段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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