CALL A FUNCTION USING TIMER IN C#

CALL A FUNCTION USING TIMER IN C#

Hi,
      Here we are going to see how to call a particular function using c#. We can do that by using           DispatcherTimer class.Do the following steps.Here i explained using simple button click event.

Step1:

      Include namespace-> using System.Windows.Threading;


Step 2:

     Create a instance for the DispatcherTimer class, at where you want to call the function.Here i created and  call with in a button click event.

        DispatcherTimer timer= new System.Windows.Threading.DispatcherTimer();
            timer.Tick += delegate(object s, EventArgs args)
            {
               //write code here for what should call after 1 sec

              //after that don't forget to call the method(where you want to stop the timer)-> timer.Stop();
            };             
                          
            timer.Interval = new TimeSpan(0, 0, 1); // one second , you can change the time here
            timer.Start(); // here the timer will be started


thats it, your code will be called based on your timer


See the example:

   I did this using silverlight.And here i am going to change background color the page dynamically.I change it by button click events. Here i created 2 buttons 


and initialize 2 variables    int start=0,stop=0;


In start button clicking i wrote some events like changing background color,and i start the timer.



In stop button click event im incrementing the stop variable value. and the above if condition will executed. then the timer will be stopped.

and run the program. and click the start button. the colors will be automatically change every 1 sec.

and when you click the stop button, the color change will be automatically stopped.
For Refrence  check this msdn link 

                                                THANK YOU FOR READING THIS!!

Comments

Popular posts from this blog

Add content type to SharePoint List/Library using REST API

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error