iggy2012 iggy2012 - 3 years ago 112
C# Question

How to make a function timedelay by 5 seconds

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;
this.Status = TransactionStatus.Complete;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download