如何使函数延时 5 秒

发布于 2024-12-17 13:09:39 字数 527 浏览 1 评论 0原文

我正在尝试将其转换为 C# 代码:

等待 5 秒,然后从银行帐户中扣除。

我有一种感觉,我已经很接近了……但这行不通。我这样做的方式正确吗?

    public override void Process(BankAccount b, decimal amount)
    {
        DateTime present = DateTime.Now;
        DateTime addFiveSeconds = DateTime.Now.AddSeconds(5);

        if (present != addFiveSeconds)
        {
            this.Status = TransactionStatus.Pending;
        }
        else
        {
            b.Debit(amount);
            this.Status = TransactionStatus.Complete;
        }
    }

I'm trying to translate this into C# code:

Wait 5 seconds and then debit the bank account.

I've got a feeling I'm close... but this isn't working. Am I doing this the right way?

    public override void Process(BankAccount b, decimal amount)
    {
        DateTime present = DateTime.Now;
        DateTime addFiveSeconds = DateTime.Now.AddSeconds(5);

        if (present != addFiveSeconds)
        {
            this.Status = TransactionStatus.Pending;
        }
        else
        {
            b.Debit(amount);
            this.Status = TransactionStatus.Complete;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你是暖光i 2024-12-24 13:09:39

使用 Thread.Sleep(5000) 来将线程挂起 5 秒,而不是您的代码 - 它有几个逻辑错误。

当执行该行时,present 将是 DateTime.Now 的值,add30Seconds 将是 DateTime.Now< 的值/code> 当该行执行时加上 5 秒。

这些变量不会更新并且不会改变它们的值。

这意味着present永远==add30Seconds

Use Thread.Sleep(5000) in order to suspend a thread for 5 seconds, instead of your code - it has several logical errors.

present will be the value of DateTime.Now when that line is executed, and add30Seconds will be the value of DateTime.Now plus 5 seconds when that line is executed.

These variables will not update and will not change their values.

This means that present will never be == to add30Seconds.

绅刃 2024-12-24 13:09:39

我认为上面的答案有缺陷,它挂起了主线程,在大多数情况下,这是危险的。
您应该为此延迟任务启动一个新线程,在线程过程中,睡眠 30 秒,然后在第二个线程中执行工作。

I think above answer has flaw, it hangs up the main thread , in most case, it is dangerous.
You should start a new thread for this delay task, in the thread proc, sleep 30 seconds and then do the work in second thread.

堇年纸鸢 2024-12-24 13:09:39

那么您的要求是等待 30 秒然后扣款?如果是这样,请查看 Timer 类。您可以将间隔设置为 30 秒,并在触发时执行您的操作。

http://www.dijksterhuis.org/using-timers-in-c/

30 秒似乎等待的时间很长?这是课程作业要求吗?

SO your requirement it wait 30 seconds and then debit? If so, look at the Timer class. You can set the interval to 30 seconds, and when triggered perform your action.

http://www.dijksterhuis.org/using-timers-in-c/

30 seconds does seem an awful long time though to wait? Is this a coursework requirement?

泪是无色的血 2024-12-24 13:09:39

您的代码中实际上没有任何等待。您将立即执行检查,并且它将始终为真。事实上,除非你非常幸运,否则你几乎永远不会达到那个准确的时间。

您可以使用计时器来实现这一点。

var completed = false;
var timer = new System.Timers.Timer(30 * 1000);
timer.Elapsed += (o,e) => 
   {
       b.Debit(amount);
       completed = true;
       this.Status = TransactionStatus.Complete;
   };
timer.Start();
this.Status = TransactionStatus.Pending;

while (!completed) ;

注意 如果借记挂起或超时,则不会进行错误检查。

There's nothing actually waiting in your code. You're immediately executing the check and it will always be true. In fact, unless you get extremely lucky, you'll almost never hit that exact time.

You could use a timer with this.

var completed = false;
var timer = new System.Timers.Timer(30 * 1000);
timer.Elapsed += (o,e) => 
   {
       b.Debit(amount);
       completed = true;
       this.Status = TransactionStatus.Complete;
   };
timer.Start();
this.Status = TransactionStatus.Pending;

while (!completed) ;

NOTE There's no error-checking if the debit hangs or times out.

魂牵梦绕锁你心扉 2024-12-24 13:09:39

您也可以使用此代码:

DateTime present = DateTime.Now;
DateTime addFiveSeconds = DateTime.Now.AddSeconds(10);

while (DateTime.Now.TimeOfDay.TotalSeconds <= addFiveSeconds.TimeOfDay.TotalSeconds)
{
    this.Status = TransactionStatus.Pending;                
}  
b.Debit(amount);
this.Status = TransactionStatus.Complete;

You can use this code too:

DateTime present = DateTime.Now;
DateTime addFiveSeconds = DateTime.Now.AddSeconds(10);

while (DateTime.Now.TimeOfDay.TotalSeconds <= addFiveSeconds.TimeOfDay.TotalSeconds)
{
    this.Status = TransactionStatus.Pending;                
}  
b.Debit(amount);
this.Status = TransactionStatus.Complete;
不爱素颜 2024-12-24 13:09:39

添加到代码开头:

使用

System.Threading;                // allows use of sleep time

在代码中插入:

Thread.Sleep(5000);             // Pause 5 seconds, where you need, delay 5 seconds

add to start of code:

using

System.Threading;                // allows use of sleep time

In your code insert:

Thread.Sleep(5000);             // Pause 5 seconds, where you need, delay 5 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文