working on it ...

Filters

Explore Public Snippets

Sort by

Found 1,265 snippets matching: timer

    public by stanchostanchev  149430  0  5  0

    Android

    how to use TimerTask
    new Timer().schedule(new TimerTask() {
    				@Override
    				public void run() {
    					// ... your code goes here ...
    				}
    			}, 1000);

    public by cghersi  2311  6  6  0

    Create and use a Timer in Java

    Thsi is the simplest way to create and use a TimerTask to do a job. You can do the job once in life, after a given delay, or repeat it
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.LinkedList;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Request {
    
    	class MYTask extends TimerTask {
    		@Override
    		public void run(){
    			//do stuff here
    		}
    	}
    
    	protected Timer timer;
    
    	public boolean scheduleTimerForPDCall(){
    		//discard any eventual currently scheduled tasks:
    		if (timer != null) {
    			timer.cancel();
    			log.info("Timer cancelled");
    		}
    		timer = new Timer(false);	//false parameter: we don't want to manage other daemon tasks...
    	
    		//schedule the task:
    		int delay = 10000; //in millisec
    		int repeatAfter = 5000; //in millisec
    		timer.schedule(new MYTask(), delay, repeatAfter);
    	
    		return true;
    	}
    }

    public by sTiLL-iLL  2607  1  8  13

    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)
    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)
        {      }
    }                        

    public by sTiLL-iLL  2616  4  7  11

    C# Threaded Observable TimePeriod

    observable timer with events, threaded and thread-safe for any usage
    
    public IObservable<long> Interval(TimeSpan waitPeriod)
    {
        return Observable.Create<long> (observer => {
            long i = 0;
            object synkMe = new object();
            return new Timer (_ => {
                lock (synkMe)
                {
                    observer.OnNext (i++);
                }
            },
            null, waitPeriod, waitPeriod);
         });
    }    
    
    void Main()
    {
        Interval (TimeSpan.FromSeconds(2.00)).Dump();
    }         

    public by SpeedOfSpin  2145  3  5  0

    SetTimeout Equivalent

    var cancellationTokenSource = new CancellationTokenSource();
    var cancellationToken = cancellationTokenSource.Token;
    
    Task.Delay(2000).ContinueWith(async (t) =>
    {
      //Do stuff
    }, cancellationToken);                      

    public by cghersi  4062  9  7  3

    Create a TimerTask in C#

    This is the simplest way to create a timertask to execute an operation in another thread without blocking the current thread
    System.Threading.Tasks.Task.Factory.StartNew(() => mymethod(parameter1)); 

    public by Magesolution  125  0  4  0

    Free Magento Daily Deal Extension

    Free Magento Daily Deal Extension is a useful tool to help you create and manage multiple deals simply and convenientlyis. You can setup countdown timer for daily deals, weekly deal or for any time period you want with this Magento Deals Extension. You are able to adjust the amount of product to sell during the promotion campaign. Deal accompanied
    https://www.magesolution.com/magento-daily-deals.html				

    public by msdn  1852  0  7  0

    SetTimer: Initialise un timer appelant la fonction à intervalles réguliers

    Initialise un timer appelant la fonction à intervalles réguliers Fonction à exécuter à intervalles réguliers Intervalle de temps du timer Renvoie un storyboard
    using System;
    using System.Collections.ObjectModel;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.IO;
    using System.Windows.Shapes;
    using System.Windows.Markup;
    
    /// <summary>
    /// Initialise un timer appelant la fonction à intervalles réguliers
    /// </summary>
    /// <param name="callback">Fonction à exécuter à intervalles réguliers</param>
    /// <param name="time">Intervalle de temps du timer</param>
    /// <returns>Renvoie un storyboard</returns>
    public static Storyboard SetTimer(Action<double, Storyboard> callback, TimeSpan time)
    {
        double t = 0;
        Storyboard timer = new Storyboard();
        timer.Duration = time;
        timer.Completed += delegate (Object sender, EventArgs args) { 
            t += time.TotalMilliseconds; 
            callback(t, timer);
            Storyboard thisTimer = (Storyboard)sender;
            thisTimer.Begin();
        };
        timer.Begin();
    
        return timer;
    }

    public by msdn  1589  0  7  0

    SetTimerOnce: Initialise un timer appelant la fonction une seule fois

    Initialise un timer appelant la fonction une seule fois Fonction à exécuter une seule fois Intervalle de temps au bout duquel la fonction sera exécutée Renvoie un timer
    using System;
    using System.Collections.ObjectModel;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.IO;
    using System.Windows.Shapes;
    using System.Windows.Markup;
    
    /// <summary>
    /// Initialise un timer appelant la fonction une seule fois
    /// </summary>
    /// <param name="callback">Fonction à exécuter une seule fois</param>
    /// <param name="interval">Intervalle de temps au bout duquel la fonction sera exécutée</param>
    /// <returns>Renvoie un timer</returns>
    public static Storyboard SetTimerOnce(Action callback, TimeSpan time)
    {
        Storyboard timer = new Storyboard();
        timer.Duration = time;
        timer.Completed += delegate (Object sender, EventArgs args) {
            callback();
        };
        timer.Begin();
    
        return timer;
    }

    external by Stefan Petersen  546  0  3  0

    How to change from Timer1 (16-bit) to Timer0 (8-bit) in AVR Arduino port and set timer tick to 1 ms.

    How to change from Timer1 (16-bit) to Timer0 (8-bit) in AVR Arduino port and set timer tick to 1 ms.: atomthread-avr-use-timer0.diff
    diff --git a/ports/avr/atomport.c b/ports/avr/atomport.c
    index 9ae5555..4107a1d 100644
    --- a/ports/avr/atomport.c
    +++ b/ports/avr/atomport.c
    @@ -277,21 +277,21 @@ void archThreadContextInit (ATOM_TCB *tcb_ptr, void *stack_top, void (*entry_poi
      */
     void avrInitSystemTickTimer ( void )
     {
    -    /* Set timer 1 compare match value for configured system tick,
    -     * with a prescaler of 256. We will get a compare match 1A
    +    /* Set timer 0 compare match value for configured system tick,
    +     * with a prescaler of 64. We will get a compare match 1A
          * interrupt on every system tick, in which we must call the
          * OS's system tick handler. */
    -    OCR1A = (AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC);
    +    OCR0A = (AVR_CPU_HZ / 64 / SYSTEM_TICKS_PER_SEC);
     
         /* Enable compare match 1A interrupt */
     #ifdef TIMSK
    -    TIMSK = _BV(OCIE1A);
    +    TIMSK = _BV(OCIE0A);
     #else
    -    TIMSK1 = _BV(OCIE1A);
    +    TIMSK0 = _BV(OCIE0A);
     #endif
     
    -    /* Set prescaler 256 */
    -    TCCR1B = _BV(CS12) | _BV(WGM12);
    +    /* Set prescaler 64 */
    +    TCCR0B = _BV(CS01) |_BV(CS00) | _BV(WGM02);
     }
     
     
    @@ -324,7 +324,7 @@ void avrInitSystemTickTimer ( void )
      *
      * @return None
      */
    -ISR (TIMER1_COMPA_vect)
    +ISR (TIMER0_COMPA_vect)
     {
         /* Call the interrupt entry routine */
         atomIntEnter();
    diff --git a/ports/avr/atomport.h b/ports/avr/atomport.h
    index 875bd15..12ff3ae 100644
    --- a/ports/avr/atomport.h
    +++ b/ports/avr/atomport.h
    @@ -40,8 +40,8 @@
     /* Definition of NULL is available from stddef.h on this platform */
     #include <stddef.h>
     
    -/* Required number of system ticks per second (normally 100 for 10ms tick) */
    -#define SYSTEM_TICKS_PER_SEC            100
    +/* Required number of system ticks per second (normally 1000 for 1ms tick) */
    +#define SYSTEM_TICKS_PER_SEC            1000
     
     /* Size of each stack entry / stack alignment size (8 bits on AVR) */
     #define STACK_ALIGN_SIZE                sizeof(uint8_t)
    
    
    • Public Snippets
    • Channels Snippets