Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Create your Own Timer using System.Threading NameSpace in .NET 2.0 using C#.

Author: DevAsp Team
Download Source Code : 698_OwnTimer.zip

In this simple article you will learn how can create your Own Timer. When you want to use timer in windows service then you can not use the form timer because Forms Namespace is not available in windo

Simply just create New Project of C#.

 

Drag two button on the form.

 

Declare some Global Variables

 

static TimerCallback timerDelegate = new TimerCallback(timerproc);

System.Threading.Timer timer01 ;

 

Now write code on Form load event

 

 private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Timer";

   

        }

 

Write two functoins

 

static void timerproc(object eventState)

        {

            try

            {

                copyThread();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        static void copyThread()

        {

            try

            {

                Class1.time = Class1.time + 1;

                MessageBox.Show("I am in My Own Timer \n Value is = " + Class1.time);

               

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message );

            }

        }

 

 

Now write code on Button Click events

 

private void button1_Click(object sender, EventArgs e)

        {

            timer01 = new  System.Threading.Timer(timerDelegate, null, 0, 3000);

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                timer01.Dispose();

            }

            catch (Exception ex)

            {

           

            }

               

        }

 

 

 

Now create new static class with this code

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace OwnTimer

{

    static class Class1

    {

        public static int time=0;

    }

}

 

 

Now your timer application is ready to run. Now you can use this time in Window Service.

Article Comments
Thanks for an easy-to-understand article..

Posted on 6/11/2010 8:03:49 AM by gags

Thanks for the easy to understand article on Threading.

Posted on 6/23/2010 1:48:21 PM by Joe @ Visual Studio Tutorial

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get Printer Current Jobs using System.Management NameSpace in .NET 2.0 using C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net