OutOfBoundsException
OutOfBoundsException

Reputation: 197

Running a program as per specified schedule using C#

Here's the scenario.
Platform: Windows
Language: C#
.NET framework: 3.5
IDE: Visual Studio 2008

I have a working executable program written in C#. It has time scheduling facility i.e. it has a numericupdown box where the user can specify the number and a combobox where the user can specify the time duration (seconds, minutes or hours). This schedule is not for the GUI but for some specific actions/processes.
This is what is happening:
The program runs for the first time. If the user has provided some time duration to perform scheduled run again for specific actions/processes then the actions are performed as per the specified schedule but only as long as the program is running. Once the program exits then the schedule is lost.
Attempt which I have made:
I have created a settings file which is saving the values for all the controls on the form. When the application starts the next time then this settings file is referred to load the values into the various controls on the form (including the time scheduling controls mentioned earlier). And I have also added the program in startup using the following code:

[RegistryPermissionAttribute(SecurityAction.LinkDemand,Write = @"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run")]
        private void startup(bool add)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            if (add)
            {
                key.SetValue("program_name", "\"" + Application.ExecutablePath + "\"");
            }
            else
                key.DeleteValue("program_name");

            key.Close();
        }


Problem:
Even though the values are getting imported from the settings file but the program is not running as per the specified time schedule.
What I intend to do:
When the program is executed the next time either manually by user or upon system start (i.e. via startup) then the previous set schedule should be referred to and program should run again as per the schedule without user intervention. Right now it requires user intervention for the first time and then runs as per schedule as long as the application is running.
Is there any way to do that? Is this what is called as making a program memory resident?

Thanks.

Upvotes: 0

Views: 819

Answers (2)

Nick
Nick

Reputation: 3337

It sounds like you are basically asking how to start a program at a specified time without user interaction.

  1. Do what Joel Coehoorn said
  2. Write a seperate program that starts your program when it needs to
  3. You could let the program sit idle and use a Thread Timer

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Rather than build this into the interface yourself, your installer should walk the user through setting this up to run using Windows Task Scheduler.

Upvotes: 2

Related Questions