Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

18 Experts available now in Live!

Get 1:1 Help Now