views:

110

answers:

2

Hi guys,

I have to a d.b entry in which the intervals are given,

Suppose 1 timer tm_5 will check the entries after every 5 mins & the timer tm_10 will check

the entries after every 10 mins,

The problem is that it checks only the entries for tm_5 not for the tm_10.

I am using C#.net 2005 & MS sql server 2005.

Guys please me with full code beacuase I am new to this field..

Thnks a lot...

Kaushal

A: 

Try this (add label1 and label2 to form named TimerTester).

public partial class TimerTester : Form
{
    public TimerTester()
    {
        InitializeComponent();
    }
    Timer _t1;
    Timer _t2;
    int _it1 = 0, _it2 = 0;
    private void Form1_Load(object sender, EventArgs e)
    {
        _t1 = new Timer() { Interval = 1000 };            
        _t1.Tick += new EventHandler(_t1_Tick);
        _t2 = new Timer() { Interval = 2000 };
        _t2.Tick += new EventHandler(_t2_Tick);
        _t1.Start(); _t2.Start();
    }
    void _t1_Tick(object sender, EventArgs e)
    {
        label1.Text = "t1: " + (_it1++).ToString();
    }
    void _t2_Tick(object sender, EventArgs e)
    {
        label2.Text = "t2: " + (_it2++).ToString();
    }        
}

Also note that you should use Forms.Timer and not Threads.Timer (or use InvokeRequired and read & understand what it implicates).

Pasi Savolainen
A: 

You should change the text of the label in the UI thread so this code will work. See Control.Invoke and Control.InvokeRequired for help.

Ikaso
which label? _________
Henk Holterman
I believe UI timer controls run on the UI thread. Your concern is only valid for separate threads. UI timers raise events on the UI thread.
BlueMonkMN
@BlueMonkMN: I didn't see that he mentioned UI Timers is his question and also the code above does not mention that.
Ikaso
Thread-based timers require a parameter on the constructor and do not implement events, therefore it's safe to assume that the code listed above is using UI timers, which have a default constructor and raise the Tick event.
BlueMonkMN