Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

c#, timer

In my console application, I will like to do something like

begin

at 1:00 am pst daily

run the program (it is just a query run with smtp email sent to the client)

end

Overall, the program is not a big deal. I just want to know how to code at 1:00 am pst daily in timer property in c#.

Thanks,
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Simply program Windows Task Scheduler to start your application at the desired time.
Avatar of ITsolutionWizard

ASKER

No I want to do it in codes
In order to do it in code, you need to create a service, that runs all the time, for an operation that happens only once a day. It not a very good idea.
I already created the service.
Avatar of Éric Moreau
create an application with a timer having the Tick event raised every 10 seconds (or your own interval) and check what time it is.
Éric Moreau: code samples please
Have you looked into Quartz.NET yet? I haven't yet used it myself, but from what I understand it is the code equivalent of Task Scheduler.
something like this:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private Timer _timer = new Timer();
        private DateTime _lastDateProcessed = DateTime.MinValue;

        public Form1()
        {
            InitializeComponent();
            _timer.Interval = 10000;
            _timer.Tick += _timer_Tick;
            _timer.Enabled = true;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if ((DateTime.Now.Hour == 1) && (_lastDateProcessed != DateTime.Today.Date))
            {
                _lastDateProcessed = DateTime.Today.Date; //to be sure we are not reprocessing in the same day
                Process.Start("FullPathToYourApplication");
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rose Babu
Rose Babu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial