Solved

c#, timer

Posted on 2014-10-14
9
194 Views
Last Modified: 2014-10-24
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
Comment
Question by:ITsolutionWizard
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 40
ID: 40381331
Simply program Windows Task Scheduler to start your application at the desired time.
0
 

Author Comment

by:ITsolutionWizard
ID: 40381337
No I want to do it in codes
0
 
LVL 40
ID: 40381537
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ITsolutionWizard
ID: 40381637
I already created the service.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40381794
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
 

Author Comment

by:ITsolutionWizard
ID: 40381857
Éric Moreau: code samples please
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40381908
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40381912
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
 
LVL 16

Accepted Solution

by:
Rose Babu earned 500 total points
ID: 40382108
"É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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
InputLanguage 1 26
REXEX help Part 2 2 25
Send SMS from VB via Sierra Wireless Modem 2 22
Dot Net 4.6.1 installation fails on W2012 R2 server 14 19
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question