每隔两分钟断开连接

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;

namespace ConsoleApp8
{
    class Program
    {
        //调用API函数
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceCounter(ref long x);
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceFrequency(ref long x);
        public static int j = 0;
        public static int back = 0;
        public static System.Timers.Timer timer = new System.Timers.Timer();
        static void Main(string[] args)
        {

            Jishi();
        }
        static void Jishi()
        {
            timer.Interval = 90;//准备计时1秒。程序中可以设置为两分钟120000
            timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            if (timer.Enabled != true)//判断计时函数是否已经启动了,若没有启动,则可以启动。否则不启动
            {
                timer.Start();
            }
            if (j == 1)//等于1说明代码为离线状态,退出方法
            {
                return;
            }
            else//继续等待下次循环
            {
                SetTime();//模拟调用接收untity请求的方法
                timer.Interval = 90;//重新准备计时1秒。程序中可以设置为两分钟120000
                timer.Start();
                Jishi();
            }
        }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            j = 1;//j等于1,表示将代码置为离线状态了
            timer.Stop();//停止计时
            return;//退出方法
        }
        /// <summary>
        /// 模拟接收untity的请求(就是个耗时操作)
        /// </summary>
        static void SetTime()
        {
            for (int i = 0; i < 330; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/HuangLiming/p/14790813.html