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

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

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
vielkacarolina1239
Asked:
vielkacarolina1239
  • 2
2 Solutions
 
elimesikaCommented:
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
 
Melih SARICACommented:

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
 
Melih SARICACommented:
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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