Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
i am trying to create time function in c# so far i have done this
class common
{

    public static int RandNumber()
    {
        Random randomNumber = new Random();
        int RN = randomNumber.Next(23,26);
        return RN;
    }

    public void TimeCheck(string handler)
    {
        Timer time = new Timer();
        time.Elapsed += new ElapsedEventHandler(handler);
        time.Interval = (1000) * (1);
        time.Enabled = true;
        time.Start();

    }


    void timer_Tick(object sender, EventArgs e)
    {
        label.Text = DateTime.Now.ToString();
    }
}


i am trying to create one common function which i can use again and again but it all ways give me error .
Posted
Comments
Herman<T>.Instance 16-Aug-11 4:19am    
which error you get?

What are you trying to achieve? Because the code you have posted does not fit together - the three parts are separate, odd, and don't appear to work.

For example, the random number function is really useless:
C#
public static int RandNumber()
{
    Random randomNumber = new Random();
    int RN = randomNumber.Next(23,26);
    return RN;
}
If you call this several times in quick succession, you will almost certainly get the same number. Move you random number generator to a class level variable instead:
C#
Random randomNumber = new Random();
public static int RandNumber()
   {
   return randomNumber.Next(23,26);
   }
Which almost asks "why have it as a separate method at all?"

Plus, don't use "magic numbers" in you code - convert them to const values with sensible names instead. That way, if they need to change, you only have to change them in one place.


Your other method is equally odd: I don't think you have thought about your task in any detail.
 
Share this answer
 
I see you are using timer from System.Timers namespace. You have not written the Elapsed event handler yet. You have written Tick event handler which comes with System.Windows.Forms.Timer class.

I guess you are new to .Net or C#. So I would suggest reading a book or two and then proceed.
 
Share this answer
 
your timer_Tick method is not invoked on the UI thread thus you cannot set the text of the label. try this:

C#
void timer_Tick(object sender, EventArgs e)
{
    if (label.InvokeRequired)
        label.BeginInvoke((MethodInvoker)(() => label.Text = DateTime.Now.ToString()));
    else
        label.Text = DateTime.Now.ToString();
}


also ElapsedEventHandler doesn't take a string, so you might want to change
C#
time.Elapsed += new ElapsedEventHandler(handler);

to
C#
time.Elapsed += timer_Tick;
 
Share this answer
 
v2
Comments
vicky87 16-Aug-11 11:46am    
thanks

but i like to create on function which tick_time and process that tick_time

public void TimeCheck(string handler)
{
Timer time = new Timer();
time.Elapsed += new ElapsedEventHandler(handler);
time.Interval = (1000) * (1);
time.Enabled = true;
time.Start();

}

i pass the values and it process it other wise i have to create many time function .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900