by
13
8
2,608
1
Top 1% !
Popular
Famous
Nice
Easy-to-find
Specified
Refined
MultiPlatform
Popularity: 6457th place
ClassNameDownCount
Languagec#
LicenseMIT_X11
MethodNameSignal
OutputTypevoid

Managed Interval! Timer Alchemy with concurrent kueue executes tasks on the b bb b bbeat

check files, ping a server, download a receipt... if you can list it this can execute it, neat and tidy. its like a gold watch of timer structs (use my non-bloking kueue  with this)
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/541436/Managed-Interval!-Timer-Alchemy-with-con?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; void Main() { var dct = new DownCount(5); new Thread (() => { for (int i = 0; i < 5; i++) { Thread.Sleep(1000); dct.Signal(); Console.WriteLine ("Signal " + i); } }).Start(); Console.WriteLine ("Wait for it..."); dc.Wait(); Console.WriteLine ("ahhhhhh......"); } public class DownCount { int val; object loka = new object (); public DownCount() { } public DownCount (int cnt1) { val = cnt1; } public void Signal() { DCnt1 (-1); } public void DCnt1 (int totl) { lock (loka) { val += totl; if (val <= 0) { Monitor.PulseAll (loka); } } } public void Wait() { lock (loka) { while (val > 0) { Monitor.Wait (loka); } } } } ////////////////////////////////////////////////////////// public class betterInterval { static void Tik(long t) { this.onTik.Invoke(t); } static IObservable<long> Interval(TimeSpan period) { return Observable.Create<long> (observer => { long i = 0; object sync = new object(); return new Timer (_ => { lock (sync) observer.OnNext (i++); }, Tik(i), period, period); }); } static IObservable<long> kreate(TimeSpan period) { return Interval (TimeSpan.FromSeconds(period)).Subscribe(onTik); } static async void onTik(t) { Console.WriteLine(t); } } void Main() { IObservable<long> bi = betterInterval.kreate(TimeSpan.FromSeconds(5)); bi.Subscribe(Console.WriteLine(t); } /////////////////////////////////////////////////////////////////////// public class KInterval { public delegate void Signal (object signal); public event Signal Received; static long tcnt = 0; long dly = 0; long prd = 0; KQue kq = new KQue(5); Timer tmr; public KInterval (long delay = 5000, long period = 3000, int workersCount = 3) { dly = delay; prd = period; if (workersCount > 0) { kq = new KQue(workersCount); } } public void Begin ( ) { kq.NQItem(( ) => tmr = new Timer(Signaled, tcnt, dly, prd)); } public void Kill ( ) { tmr.Dispose(); kq.ShutdownKQ(true); } private void Signaled (object signal) { var sig = (long) signal; var st = sig; this.Received += KInterval_Received; this.Received.Invoke(sig); this.Received -= KInterval_Received; } void KInterval_Received (object signal) { } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus