Skip to main content

Posts

Showing posts from November, 2019

A better sleep method than Thread.Sleep in C sharp

Sleep.CS: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TimerNoCpuConsume {     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 = CharSet.Auto)]         [return: MarshalAs(UnmanagedType.Bool)]         static extern bool CloseHandle(IntPtr hObject);         [DllImport("User32.dll")]         static extern int Msg