Solved

how to create a job schedule using C# in a console application?

Posted on 2008-06-12
3
8,123 Views
Last Modified: 2013-12-10
Hi,

I am using C# in visual studio 2005

I need to create a console application that needs to run at 5:00 pm and run a process once per day. I have the following code. Please, see code snippet. however, this is looping all the time and kills the cpu. Please, how can I do the same thing but more efficient. How can I create a windows service that will run every day at 5:00pm and execute my process when is 5:00pm only?

Thanks.
class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                if (DateTime.Now.Hour == 17)
                {
                   myMethod();
                }
            }
        }
 
        static void myMethod()
        {
          //do something
        }
    }

Open in new window

0
Comment
Question by:vielkacarolina1239
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 450 total points
ID: 21774313
HI

This is not the right way to do that , follow

1) create your exe such that the Main includes only the code that was in My method (see code below)

Now , use windows scheduler to call your EXE each day at 17:00

refer to this manual :
http://www.iopus.com/guides/winscheduler.htm
class Program
    {
        static void Main(string[] args)
        {
          //do something
        }
    }

Open in new window

0
 
LVL 19

Assisted Solution

by:Melih SARICA
Melih SARICA earned 50 total points
ID: 21774324

using System;
using System.Threading;
 
public class ThreadWork 
{
   public static void DoWork()
   {
while(true)
            {
                if (DateTime.Now.Hour == 17&DateTime.Now.Minute==0&DateTime.Now.Second==0)
                {
                   myMethod();
              Thread.Sleep(1000*60);
                }
              Thread.Sleep(1000*60);
            }
   }
}
class ThreadTest
{
   public static void Main()
   {
      ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
      Thread myThread = new Thread(myThreadDelegate);
      myThread.Start();
   }
}

Open in new window

0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 21774336
and what Elimesika  is exectly true...
if u try to solve it with code.. what happens when ur code crashes.. it wont run till u restart ur application
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

System overheating may become a serious problem if not taken care of at the proper time. I am writing this article because I faced a similar problem. Intro All electronic devices produce heat, but computers are a special case - the processors bo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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