实现方法是通过使用计时器,等待消息抵达,在等待过程中转让windows权限,可以达到延时目的(参考了其他例程),而且不影响窗口卡死。
这个引用需要手动导入,如果不需要考虑窗口卡死,可以去掉,不影响延时效果
System.Windows.Forms
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ConsoleApp2
{
public class Timer
{
public delegate void TimerCompleteDelegate();
[DllImport("kernel32.dll")]
static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll")]
static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long ft, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume);
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSe