• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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,
0
ITsolutionWizard
Asked:
ITsolutionWizard
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Jacques Bourgeois (James Burger)PresidentCommented:
Simply program Windows Task Scheduler to start your application at the desired time.
0
 
ITsolutionWizardAuthor Commented:
No I want to do it in codes
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ITsolutionWizardAuthor Commented:
I already created the service.
0
 
Éric MoreauSenior .Net ConsultantCommented:
create an application with a timer having the Tick event raised every 10 seconds (or your own interval) and check what time it is.
0
 
ITsolutionWizardAuthor Commented:
Éric Moreau: code samples please
0
 
käµfm³d 👽Commented:
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.
0
 
Éric MoreauSenior .Net ConsultantCommented:
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

0
 
Rose BabuSenior Team ManagerCommented:
"Éric Moreau" has give a clear picture of the solution interms of winforms ;-) Have this for the console application...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp_CSHARP
{
    class Program
    {

        private static System.Timers.Timer aTimer;

        private static void SendEmail(Object source, System.Timers.ElapsedEventArgs e)
        {
            if (DateTime.Now.ToString("HHmmss") == "010001") // 01:00:01 AM
            {
                // Place the email sending code here
                bool isEmailSuccess = true;
                if (isEmailSuccess)
                {
                    Console.WriteLine("Email Successfully sent at " + DateTime.Now.ToString("HHmmss"));
                }
                else 
                {
                    Console.WriteLine("Email failed at " + DateTime.Now.ToString("HHmmss"));
                }
            }
        }

	// This application should be in running state at the time of 01:00:01
        static void Main(string[] args)
        {
            // Create a timer with a 1 second interval.
            aTimer = new System.Timers.Timer(1000);
            // POINT THE EMAIL FUNCTION TO BE CALLED. 
            // CHECK THE TIME AND THEN CONTINUE EMAILING IN THAT EMAIL FUNCTION 
            aTimer.Elapsed += SendEmail;
            aTimer.Enabled = true;
        }        
    }
}

Open in new window

Hope this helps.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now