Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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