Saturday, 06 September 2008
DotNetSpace, Asp.net 2.0 tutorials and code examples. ajax, log4net...
Home arrow Articles arrow General Articles arrow How to configure a Timer object
 
Main Menu
Home
Articles
News
Links
Search








BiraJones




How to configure a Timer object Print E-mail
Tuesday, 24 October 2006

This code shows how to configure a timer object to call determined method at some defined intervals:

//Fragment class code. Se below the entire class.

public void somemethod(....)
{
    //The basic idea is:
    //Create Timer object
    Timer myTimer = new Timer();
    myTimer.Interval = 10;
    //set method to be called
    myTimer.Elapsed += new ElapsedEventHandler(tobecalledmethod);
    myTimer.Enabled = true;
    pathAtual = atualPath;
}

public void tobecalledmethod(....)
{
//stuff called at some interval
}

//Full Timer class example:

using System;
using System.Data;
using System.Configuration;
using System.Timers;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


/// <summary>
/// Responsible for time based processes.
/// </summary>
public class AppTimer
{
public void start(int interval)
    {
        log.Debug("Starting timer");

        Timer myTimer = new Timer();
        myTimer.Interval = interval;
        myTimer.Elapsed += new ElapsedEventHandler(readSomething);
        myTimer.Enabled = true;
    }

    private void readSomething(object sender, System.Timers.ElapsedEventArgs e)
    {
        //do stuff
    }
}

Last Updated ( Tuesday, 31 October 2006 )
 
< Prev   Next >
Popular
Polls
-->*/?>

Which language will you choose for coding with Visual Studio .NET?
 
-->*/?>

Is ASP.NET better than Java / JSP frameworks?
 

© 2008 DotNetSpace
Joomla! is Free Software released under the GNU/GPL License.