-5

我正在使用套接字编程,我可以在一些时间间隔(例如 10 秒)后检查用户的实时连接。但目前,我不知道。我会怎么做。

请帮我。我将非常感谢。

4

3 回答 3

3

从 MSDN计时器类(系统计时器)

下面是来自 MSDN 页面的示例

[C#] 
public class Timer1
{
    public static void Main()
    {
        System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
        // Set the Interval to 10 seconds.
        aTimer.Interval=10000;
        aTimer.Enabled=true;

        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }

    // Specify what you want to happen when the Elapsed event is raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}
于 2013-03-05T14:47:56.163 回答
2

包括:using System.Threading;

我不知道你在做什么(有代码给我们看?),但这是一般逻辑:

while (/*connection is active*/)
{
 //check connection
 Thread.Sleep(10000); //10 seconds
}
于 2013-03-05T14:46:27.930 回答
1

It really depends on what your doing, and how you are checking for the connection.

You can use the Threading.sleep(); a timer, or depending on what your doing you might be able to use an event handler based off of connects / disconnects...

于 2013-03-05T14:54:12.217 回答