Task Scheduler with Window Form

Paul_123

New member
Joined
May 7, 2015
Messages
4
Programming Experience
1-3
Is it possible to run a Windows Form application and I also want to run it as windows task scheduler ?. Let me explain it slowly:

1. The app can run very just like regular Windows form application
2. When it's setup to run as scheduler task, it will not pop up the app on computer but quietly run on the backup and shut down when it's done.

Thanks,
 

JuggaloBrotha

Staff member
Joined
Apr 23, 2011
Messages
177
Location
Lansing, MI; USA
Programming Experience
10+
Have you considered using commandline args?
You can have your app look for a commandline arg indicating it's ran from the scheduled task (set the task to send the commandline arg) and if there aren't any args then it's a user who ran the program.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,792
Location
Sydney, Australia
Programming Experience
10+
Have you considered using commandline args?
You can have your app look for a commandline arg indicating it's ran from the scheduled task (set the task to send the commandline arg) and if there aren't any args then it's a user who ran the program.

That is definitely how you should determine how the application was invoked. You can then choose whether to display a form or not in the Main method. The Main method looks like this by default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
You can change it to something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0 || args[0] != "/silent")
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                WorkSilently();
            }
        }

        static void WorkSilently()
        {
            Console.WriteLine("Working silently");
        }
    }
}
If you run the application as you usually do then it will display the form as it usually does. If you set your scheduled task to use a commandline like "C:\MyApp.exe /silent" then it will not display a form but do the work specified on the alternate path.

Note that you can test that in the IDE by setting the commandline parameter on the Debug page of the project properties.
 
Top Bottom