tags:

views:

1788

answers:

3

How can i generate an event on a specific time say i want to generate alert at 8:00 AM that its 8:00 AM and on any given time of clock

+8  A: 

Use the Timer class:

var dt = ... // next 8:00 AM from now
var timer = new Timer(callback, null, dt - DateTime.Now, TimeSpan.FromHours(24));

The callback delegate will be called the next time it's 8:00 AM and every 24 hours thereafter.

See this SO question how to calculate the next 8:00 AM occurrence.

dtb
I know following the link mentions it, but might want to clarify that that is System.Threading.Timer and not System.Windows.Forms.Timer
McAden
A: 
private void Run_Timer()
    {
        DateTime tday = new DateTime();
        tday = DateTime.Today;
        TimeSpan Start_Time = new TimeSpan(8,15,0);
        DateTime Starter = tday + Start_Time;
        Start_Time = new TimeSpan(20, 15, 0);
        DateTime Ender = tday + Start_Time;
        for (int i = 0; i <= 23; i++)
        {
            Start_Time = new TimeSpan(i, 15, 0);
            tday += Start_Time;
            if (((tday - DateTime.Now).TotalMilliseconds > 0) && (tday >= Starter) && (tday <= Ender))
            {
                Time_To_Look = tday;
                timer1.Interval = Convert.ToInt32((tday - DateTime.Now).TotalMilliseconds);
                timer1.Start();
                MessageBox.Show(Time_To_Look.ToString());
                break;
            }
        }
    }

We can use this function for getting timer running for times or change it to run on a specific time :D

Mobin
It's obvious that @dtb solution is better.
AlbertEin
But its also obvious that you didn't get the idea that a person who asks the question better understand his question .I agree he gave me a format for doing that on one specific time of day but i had to make it run on some conditions not statically on a certain time of day once most of the work done in my code is for running that loop and set the time for next timer stop .If you can see that not the number of lines in that
Mobin
+1  A: 

Use a Scheduled Task to generate the alert.

ChrisF