简体   繁体   中英

How to identify the process is started manually or from Task Scheduler?

I have a win form application which generates some PDF files. Now I have to create a task in scheduler to run the application on every day on specified time. Now what I have to do is, I need to run the application manually. While its running manually need to show some extra results to user. So how can I identify the application run by scheduler or manually?

Here You Go

  1. Go to properties of project set some command line arguments. This will be for knowing manually (set for both release and debug) 在此处输入图片说明

  2. Now go to Task Scheduler and set parameters like given below 在此处输入图片说明

  3. Now when it runs from exe or scheduler this argument will come as parameter

    Code Sample

     static void Main(string[] args) { Console.WriteLine(args[0]); } 

You may use current directory as an indicator:

if (Environment.CurrentDirectory == Application.StartupPath)
{
    // Started from Start menu
}
else if (Environment.CurrentDirectory == Environment.SystemDirectory)
{
    // Started by Task Scheduler
}

It works when current directory ("Start in") is set to application directory in start menu shortcut and not set in Task Scheduler action. This is pretty common.

Application Directory set as current directory in start menu shortcut

Remark: Ramankingdom's answer is preferable. Though, if for some (organizational) reason you cannot use command-line parameters, this is an additional option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM