C# 最多等待5秒钟

C# 最多等待5秒钟,c#,.net-3.5,C#,.net 3.5,我试图实现一些等待布尔值为真的东西。如果5秒后布尔值仍然不为真,那么我将执行错误消息代码 这就是我现在正在做的。但是这种方法在所有情况下只需等待5秒钟,这是在浪费时间。我怎样才能在变量变为真时执行这样的操作 Thread.Sleep(5000); if (imageDisplayed == true) { //success } else { //failed } 把你的睡眠分成“小睡” for(int n=0;n

我试图实现一些等待布尔值为真的东西。如果5秒后布尔值仍然不为真,那么我将执行错误消息代码

这就是我现在正在做的。但是这种方法在所有情况下只需等待5秒钟,这是在浪费时间。我怎样才能在变量变为真时执行这样的操作

Thread.Sleep(5000);
if (imageDisplayed == true) {
    //success
}
else {
    //failed
}
把你的睡眠分成“小睡”

for(int n=0;n<50;n++)
{
睡眠(100);
如果(显示图像)
{
//成功
打破
}
}
//失败
不是很快,但最大延迟为100毫秒。

将睡眠打断为“小睡”。)

for(int n=0;n<50;n++)
{
睡眠(100);
如果(显示图像)
{
//成功
打破
}
}
//失败

不是很快,但最大延迟为100ms。

您可以将超时变量设置为要停止等待的时间,并将其与正在等待的检查一起用作while循环中的条件。在下面的示例中,我们只需在检查之间睡眠十分之一秒,但您可以根据需要调整睡眠时间(或将其删除):

var timeout = DateTime.Now.AddSeconds(5);

while (!imageDisplayed && DateTime.Now < timeout)
{
    Thread.Sleep(100);
}

// Here, either the imageDisplayed bool has been set to true, or we've waited 5 seconds
var timeout=DateTime.Now.AddSeconds(5);
而(!imagedisplated&&DateTime.Now
您可以将超时变量设置为要停止等待的时间,并将其与等待的检查一起用作while循环中的条件。在下面的示例中,我们只需在检查之间睡眠十分之一秒,但您可以根据需要调整睡眠时间(或将其删除):

var timeout = DateTime.Now.AddSeconds(5);

while (!imageDisplayed && DateTime.Now < timeout)
{
    Thread.Sleep(100);
}

// Here, either the imageDisplayed bool has been set to true, or we've waited 5 seconds
var timeout=DateTime.Now.AddSeconds(5);
而(!imagedisplated&&DateTime.Now
使用while循环并递增地检查您的状况

var waitedSoFar = 0;
var imageDisplayed = CheckIfImageIsDisplayed(); //this function is where you check the condition

while(waitedSoFar < 5000)
{
   imageDisplayed = CheckIfImageIsDisplayed();
   if(imageDisplayed)
   {
      //success here
      break;
   }
   waitedSoFar += 100;
   Thread.Sleep(100);
}
if(!imageDisplayed)
{
    //failed, do something here about that.
}
var waitedSoFar=0;
var imageDisplayed=CheckIfImageIsDisplayed()//此功能用于检查条件
同时(等待时间<5000)
{
imageDisplayed=CheckIfImageIsDisplayed();
如果(显示图像)
{
//在这里成功
打破
}
waitedSoFar+=100;
睡眠(100);
}
如果(!图像显示)
{
//失败了,在这里做点什么。
}

使用while循环并递增地检查您的状况

var waitedSoFar = 0;
var imageDisplayed = CheckIfImageIsDisplayed(); //this function is where you check the condition

while(waitedSoFar < 5000)
{
   imageDisplayed = CheckIfImageIsDisplayed();
   if(imageDisplayed)
   {
      //success here
      break;
   }
   waitedSoFar += 100;
   Thread.Sleep(100);
}
if(!imageDisplayed)
{
    //failed, do something here about that.
}
var waitedSoFar=0;
var imageDisplayed=CheckIfImageIsDisplayed()//此功能用于检查条件
同时(等待时间<5000)
{
imageDisplayed=CheckIfImageIsDisplayed();
如果(显示图像)
{
//在这里成功
打破
}
waitedSoFar+=100;
睡眠(100);
}
如果(!图像显示)
{
//失败了,在这里做点什么。
}
最好使用一个

// Somewhere instantiate this as an accessible variable to both 
// display logic and waiting logic.
ManualResetEvent resetEvent = new ManualResetEvent(false);

// In your thread where you want to wait for max 5 secs
if(resetEvent.WaitOne(5000)) {   // this will wait for max 5 secs before continuing.
    // do your thing
} else {
    // run your else logic.
}

// in your thread where you set a boolean to true
public void DisplayImage() {
    // display image
    display();

    // Notify the threads waiting for this to happen
    resetEvent.Set();   // This will release the wait/lock above, even when waiting. 
}
经验法则。最好不要在生产代码中使用sleep,除非您有非常、非常、非常好的理由这样做

最好使用一个

// Somewhere instantiate this as an accessible variable to both 
// display logic and waiting logic.
ManualResetEvent resetEvent = new ManualResetEvent(false);

// In your thread where you want to wait for max 5 secs
if(resetEvent.WaitOne(5000)) {   // this will wait for max 5 secs before continuing.
    // do your thing
} else {
    // run your else logic.
}

// in your thread where you set a boolean to true
public void DisplayImage() {
    // display image
    display();

    // Notify the threads waiting for this to happen
    resetEvent.Set();   // This will release the wait/lock above, even when waiting. 
}

经验法则。最好不要在生产代码中使用sleep,除非您有非常、非常、非常好的理由这样做

听起来你想使用这个类

设置布尔变量,使其在设置为true时执行函数

    System.Timers.Timer t;
    private bool val;
    public bool Val {
        get { return val; }
        set
        {
            if (value == true)
                // run function here
            val = value;
        }
    }
然后每5秒设置一次计时器间隔

    public Main()
    {
        InitializeComponent();

        t = new System.Timers.Timer(5000);

        t.Elapsed += T_Elapsed;
    }

    private void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        throw new Exception();
    }

要启动计时器,只需使用
t.start()
t.Reset()
重置计时器

听起来像是要使用该类

设置布尔变量,使其在设置为true时执行函数

    System.Timers.Timer t;
    private bool val;
    public bool Val {
        get { return val; }
        set
        {
            if (value == true)
                // run function here
            val = value;
        }
    }
然后每5秒设置一次计时器间隔

    public Main()
    {
        InitializeComponent();

        t = new System.Timers.Timer(5000);

        t.Elapsed += T_Elapsed;
    }

    private void T_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        throw new Exception();
    }

要启动计时器,只需使用
t.start()
t.Reset()
来重置计时器

你应该做一个while循环,一旦它到达时间,就做这个动作。你应该做的是改变实际正在做工作的代码,以做一些比设置布尔值来指示何时完成更好的事情,例如调用回调、返回指示何时完成的
任务
、触发事件等。您应该为此执行while循环,并在它到达时间时立即执行操作。您应该做的是更改实际执行任务的代码,以执行比设置布尔值指示何时完成要好得多的操作,例如调用回调、返回指示任务何时完成的
任务、触发事件等
val
是否应该
imagedisplay
,如示例中所示?是的,这是正确的,我只是解释了理论应该
val
应该
imagedisplay
,如示例中所示?是的,这是正确的,我只是简单地解释了theoryThanks,但你忘了重置吗?我是否可以使用
自动重置事件
执行此操作?您可以在设置后调用resetEvent.reset,根据需要进行重置。使用AutoResetEvent,设置时您没有控制权,因此这对您的情况不是很好。谢谢,但您是否忘记了重置?我是否可以使用
自动重置事件
执行此操作?您可以在设置后调用resetEvent.reset,根据需要进行重置。在设置AutoResetEvent时,您没有控制权,因此这对于您的案例来说并不是很好。