Solved

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

Posted on 2008-06-12
3
8,073 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
  • 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

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now