Harsh
Harsh

Reputation: 131

How to excecute a function for a specific time?

In my application, I am using socket while receiving acknowledgment. I have to check my function for certain amount of time (lets say for x no of seconds) for acknowledgment.

if acknowledgment not received then wait for x no. of seconds wait till the given amount of time.

What is the best way to do this?

Upvotes: 0

Views: 79

Answers (1)

Arihant Lodha
Arihant Lodha

Reputation: 46

Use Stop Watch to execute a function for a particular time.

//Below code will excecute the code for 10 sec 
Stopwatch stopWatch = new Stopwatch();



 do
 {
  stopWatch.Start();

//perform your Function

    if (result = true)
    {
    stopwatch.reset();
    break;
    } 

    }While (stopWatch.Elapsed.Seconds <= 10));

Upvotes: 1

Related Questions