简体   繁体   English

在 C# 中以编程方式创建的 Windows 任务计划程序未自动运行

[英]Windows task scheduler created programmatically in C# not running automatically

I have created windows task scheduler programmatically in c#. Task is created successfully and is scheduled to run correctly.我在 c# 中以编程方式创建了 windows 任务调度程序。任务已成功创建并计划正确运行。 At scheduled time, it says task is running but without any result and next schedule time is updated.在预定时间,它表示任务正在运行但没有任何结果,并且更新下一个预定时间。

But last run time and last run result does not update.但上次运行时间和上次运行结果不会更新。

Last run result is: The task has not yet run.(0x41303)上次运行结果为:任务尚未运行。(0x41303)

But when run manually from task scheduler it executes successfully but not automatically.但是当从任务计划程序手动运行时,它会成功执行但不会自动执行。

Below code that i used to create task下面是我用来创建任务的代码

var ts = new TaskService();
var td = ts.NewTask();
td.RegistrationInfo.Author = "My company";
td.RegistrationInfo.Description = "Runs test application";
var trigger = new WeeklyTrigger { StartBoundary = startDate, DaysOfWeek = daysOfWeek, Enabled = enabled };
trigger.Repetition.Interval = TimeSpan.FromSeconds(((minutes == 0) ? 60 : minutes) * 60);
td.Triggers.Add(trigger);
var action = new ExecAction(Assembly.GetExecutingAssembly().Location, null, null);
if (filePath != string.Empty && File.Exists(filePath))
{
    action = new ExecAction(filePath);
}
action.Arguments = "AutoRun";
td.Actions.Add(action);
ts.RootFolder.RegisterTaskDefinition(TaskName, td);

Any help would be much appreciated!任何帮助将非常感激!

Turns out, for running any task in scheduler, laptop charger must be plugged in else scheduler does not execute the task.事实证明,要在调度程序中运行任何任务,笔记本电脑充电器必须插入,否则调度程序不会执行任务。

This is not the case with windows server or desktop systems. windows 服务器或桌面系统不是这种情况。

Not sure about this behavior but this is what i figured it out.不确定这种行为,但这就是我想出来的。

Check the execution privileges first.首先检查执行权限。 Then check the task manager if the process is really running when it seems 'running'.然后在进程看起来“正在运行”时检查任务管理器是否真的在运行。 If yes, try to use some try-catch blocks and create event logs as exceptions.如果是,请尝试使用一些 try-catch 块并创建事件日志作为例外。

I think when you run manually from task scheduler, its executed by a user that belongs to task scheduler (maybe administrator).我认为当您从任务调度程序手动运行时,它由属于任务调度程序的用户(可能是管理员)执行。 But at scheduled time, application trying to be executed as a user that won't have enough privileges to do some stuff in your code.但是在预定的时间,应用程序试图以没有足够权限在代码中执行某些操作的用户身份执行。

UPDATE更新

  1. Set Start in (optional) value to target file location.将 Start in(可选)值设置为目标文件位置。 Without it, the task scheduler runs in system32 folder but like i said before, target application wouldn't have privileges to run in system32.没有它,任务调度程序会在 system32 文件夹中运行,但就像我之前所说的,目标应用程序将没有在 system32 中运行的权限。
  2. Try to change the version of the console application to 32 bit.尝试将控制台应用程序的版本更改为 32 位。

    ie Right click Goto -> Properties -> Build -> Platform Target = x86.即右键单击 Goto -> Properties -> Build -> Platform Target = x86。

For me the issue was the executable crashing with "Application Error".对我来说,问题是可执行文件因“应用程序错误”而崩溃。 You can't see any error in Task scheduler.您在任务计划程序中看不到任何错误。 It will just show last run result as "The task has not yet run.(0x41303)"它只会将上次运行结果显示为“任务尚未运行。(0x41303)”

TO get the error please check Event Viewer要获取错误,请检查事件查看器

EventViewer -> Windows Logs -> Aplication事件查看器 -> Windows 日志 -> 应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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