简体   繁体   English

从TaskScheduler运行时,如何查找/关闭进程?

[英]How do you Find/Close process when running from TaskScheduler?

I have two programs that conflict with each other. 我有两个相互冲突的程序。 One of them is a C# console app that is launched by Task Scheduler (Program1) in the middle of the night, the other (Program2) is a C# WPF program that is typically run by users during the day. 其中一个是由任务计划程序(Program1)在深夜启动的C#控制台应用程序,另一个(Program2)是通常由用户在白天运行的C#WPF程序。 I attempted to use the following to shut down Program2 from Program1: 我试图使用以下命令从Program1中关闭Program2:

public static void CloseProgram2()
{
    var process = System.Diagnostics.Process.GetProcessesByName("Program2").FirstOrDefault();
    if (process != null)
    {
        if (process.CloseMainWindow())
        {
            SpinWait.SpinUntil(() => null == System.Diagnostics.Process.GetProcessesByName("Program2").FirstOrDefault(),
                TimeSpan.FromSeconds(30));
        }
    }
}

For some reason, Program2 does not shut down when Program1 is run from the TaskScheduler. 由于某些原因,从TaskScheduler运行Program1时,Program2不会关闭。 However, if I run Program1 manually, it always shuts down Program2. 但是,如果我手动运行Program1,它将始终关闭Program2。 I have task scheduler set to provide credentials so that Program1 will run whether or not anybody is logged into the computer. 我将任务计划程序设置为提供凭据,以便无论是否有人登录到计算机,Program1都将运行。 I notice that the output window for Program1 is not displayed while it is running from Task Scheduler, but it does successfully run. 我注意到从Task Scheduler运行时,Program1的输出窗口没有显示,但确实运行成功。 I am wondering if perhaps Task Scheduler is running Program1 in such a way that it does not see processes that were not started up by Task Scheduler, and that's why Program2 doesn't get shut down. 我想知道Task Scheduler是否以某种方式运行Program1,使其看不到不是由Task Scheduler启动的进程,这就是为什么Program2无法关闭的原因。

Any ideas or solutions that can help me close Program2 from Program1 when Program1 is run using TaskScheduler would be greatly appreciated. 当使用TaskScheduler运行Program1时,可以帮助我从Program1关闭Program2的任何想法或解决方案将不胜感激。

Found another solution to use. 找到了另一个可以使用的解决方案。 Instead of using proccess.CloseMainWindow() I created a batch file with the following: 我没有使用proccess.CloseMainWindow(),而是使用以下命令创建了一个批处理文件:

taskkill /F /IM Program2.exe

And then just ran that batch file from Task Scheduler. 然后只需从Task Scheduler运行该批处理文件。

What's weird, is that I needed to put a blank line before the taskkill command in my bat file. 奇怪的是,我需要在bat文件中的taskkill命令前加一个空白行。 Without it, I get a bunch of gibberish and it says it doesn't know how to run that command. 没有它,我会产生一堆胡言乱语,它说它不知道如何运行该命令。 Almost as if garbage was inserted before the taskkill command. 几乎就像在taskkill命令之前插入了垃圾一样。 Very strange... 很奇怪...

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

相关问题 如何从自定义TaskScheduler回退到Default TaskScheduler - How do I fallback to the Default TaskScheduler from a custom TaskScheduler 部署 .NET 应用程序时 - 您如何找出共享相对于运行应用程序的计算机所在的区域? - When deploying .NET applications - how do you find out what zone a share is in relative to the computer running the application? 如何找到正在运行的进程的窗口句柄? - How do I find the window handle for a running process? 如何使用 C# 从 windows Temp 文件夹中删除所有文件? (当进程运行时) - How do you delete all files from the windows Temp folder using C#? (When the processes are running) 如何在运行单元测试时禁用PostSharp? - How do you disable PostSharp when running unit tests? 关闭表单时仍在运行的后台工作程序会发生什么? - What happens to a background worker that is still running when you close the form? 你如何从进程内调用另一个进程 c# - How do you call another process from within a process c# 如何从不同的进程关闭窗口 - how to close a window from a different process 如何从c#关闭另一个进程 - How to Close another Process from c# 如何识别和关闭在文件C#.net上运行的进程 - How to identify and close a process running on a file c# .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM