如何在特定时间执行功能?

How to excecute a function for a specific time?

在我的应用程序中,我在接收确认时使用套接字。 我必须在一定时间内检查我的功能(假设为 x 秒)以获得确认。

如果未收到确认,请等待 x 否。等待给定时间的秒数。

最好的方法是什么?

使用秒表在特定时间执行功能。

//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));