简体   繁体   English

C#服务调用未关闭的控制台应用程序

[英]C# Service calls Console Application that doesn't close

What I have is a windows Service calling a console application that i'm running. 我所拥有的是一个Windows Service,它正在调用我正在运行的控制台应用程序。 However, when the service run it again, the console app doesn't close. 但是,当该服务再次运行时,控制台应用程序不会关闭。 is it best to have the app close itself when it's done running or have the service close it? 最好是在应用程序运行完毕后关闭自身或让服务关闭它? In either cause can you give an example on how to close it? 无论哪种原因,您都可以举例说明如何关闭它吗?

while (true)
        {
            try
            {
                string ectory = @"C:\Program Files\Checker.exe";
                EventLog.WriteEntry("PriceGrab", "Calling executeable");
                var p = Process.Start(ectory);
                if (!p.WaitForExit(30000))
                {

                    p.Kill();
                }
                System.Threading.Thread.Sleep(600000); // wait 10 minutes
            }
            catch (Exception ex)
            {

                EventLog.WriteEntry("PriceGrabCall", ex.Message, EventLogEntryType.Warning);

            }

This is what I have inside of my Service executable. 这就是我的Service可执行文件中的内容。 This will not close the app. 这不会关闭应用程序。 The app is designed to run once every 10 minutes. 该应用程序旨在每10分钟运行一次。 N/M works now... N / M现在可以使用...

It depends on the nature of the console app. 这取决于控制台应用程序的性质。 If it's like a server app and it won't quit (has an infinite loop...), then you just start it once and only kill it when you don't need it anymore (or just leave it running...). 如果它像一个服务器应用程序并且不会退出(具有无限循环...),那么您只需启动它一次,然后在不再需要它时将其杀死(或使其保持运行状态...)。 。 If it's supposed to exit, you can give it some time to close itself, and then kill it if it didn't finish: 如果应该退出,则可以给它一些时间使其自行关闭,然后在未完成时将其杀死:

var p = Process.Start( ... );

// ...

if (!p.WaitForExit(5000)) { // wait 5 seconds
  p.Kill();
}

But be careful when killing processes like this. 但是在终止此类进程时要小心。 You might lose the work that they were doing. 您可能会失去他们正在做的工作。

只是一个可能更可靠的建议:我过去做过类似的事情,并使用了一种解决方案,其中,每隔5分钟左右通过Windows Task Scheduler调度控制台应用程序,并检查该控制台应用程序创建的新文件使用文件系统监视程序。

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

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