简体   繁体   English

C#-暂停主线程并在进程退出之前清理

[英]C# - Pause main thread and clean up before process exit

How can I execute some code when my process is going to exit? 我的流程即将退出时,如何执行一些代码? I've tried this: 我已经试过了:

public static void Main(string[] args) {

    StartUpMyServer();

    AppDomain.CurrentDomain.ProcessExit += delegate(object sender, EventArgs e)
    {
        Console.WriteLine("process exiting!");
        TellMyServerToStopAcceptingConnectionsAndWaitUntilItsFinishedWithAnyExistingOnes();
    };

    Thread.Sleep(Timeout.Infinite);

}

"process exiting" is never printed and nothing else seems to happen. 永远不会打印“进程退出”,似乎什么也没有发生。

Whatever it is, it needs to be cross-platform (Mono/Linux/OS X/Windows). 无论是什么,它都必须是跨平台的(Mono / Linux / OS X / Windows)。

Thanks! 谢谢!

Clarification: I could probably accomplish what I want to do by starting and stopping the server on a separate thread and polling a 'stop requested' flag (which would be set in the ProcessExit event hander) on the main thread every X ms. 澄清:我可以通过在单独的线程上启动和停止服务器,并每隔X ms轮询主线程上的“停止请求”标志(将在ProcessExit事件处理程序中设置)来完成我想做的事情。 I'd like to avoid this solution if possible (extra thread, cpu cycles). 如果可能的话,我想避免这种解决方案(额外的线程,CPU周期)。 I just want to main thread to suspend until such time as the process is exiting, when it should tell the server to stop. 我只想让主线程挂起,直到该进程退出时才应该告诉服务器停止。 Possible? 可能?

Your Thread.Sleep is blocking. 您的Thread.Sleep正在阻止。 You will have to think of a better idea. 您将不得不考虑一个更好的主意。 Without knowledge how your server runs, I cant help. 不知道您的服务器如何运行,我无济于事。

I have encountered a similar problem. 我遇到了类似的问题。 Where I work, we have a set of libraries that get used by every client-side application we write, which contains cleanup code that needs to be executed upon application exit. 在我工作的地方,我们有一组库可供编写的每个客户端应用程序使用,其中包含需要在应用程序退出时执行的清除代码。 AppDomain.CurrentDomain.ProcessExit works great for us, and we have to use that method because we can't rely on the top-level application (running Program.Main) to explicitly call our cleanup method, nor do we have any control over that (since it's not our code, it belongs to the user using our libraries). AppDomain.CurrentDomain.ProcessExit对我们来说非常有用,我们必须使用该方法,因为我们不能依赖顶级应用程序(运行Program.Main)来显式调用我们的cleanup方法,也不能对此进行任何控制(由于它不是我们的代码,因此它属于使用我们库的用户)。

However... in your situation, I don't think you need to worry about a ProcessExit event to fire your cleanup code. 但是...根据您的情况,我认为您无需担心ProcessExit事件来触发您的清理代码。 You're writing some sort of network server, hosted in a console application. 您正在编写某种托管在控制台应用程序中的网络服务器。 How about: 怎么样:

public static void Main(string[] args)
{
  StartUpMyServer();

  Console.WriteLine("Press the ESCAPE key to shut down the server.");
  while (Console.ReadKey(true).Key != ConsoleKey.Escape);

  ShutDownMyServer();
}

Then when the user presses ESCAPE, your server will be shut down. 然后,当用户按下ESCAPE时,您的服务器将关闭。 The really elegant way to do this for a networked server would be to create a Windows service, which will give you handy OnStart() and OnStop() methods, where you just include your appropriate start-up and shut-down code, and the Windows service manager handles calling those for you. 为网络服务器执行此操作的一种非常优雅的方法是创建Windows服务,该服务将为您提供方便的OnStart()和OnStop()方法,其中仅包含您合适的启动和关闭代码,并且Windows服务管理器会为您处理这些调用。 Ultimately, the real problem is ensuring that you're not sleeping forever... you need some way to signal your application to exit. 最终,真正的问题是确保您不会永远睡觉……您需要某种方式来通知您的应用程序退出。 There are many ways to do it, it all depends on how you're intending your app to run. 有很多方法可以做到,这完全取决于您打算如何运行应用程序。

Implement IDisposable on the objects that need cleanup, and Dispose() them as the application/server shuts down. 在需要清理的对象上实现IDisposable ,并在应用程序/服务器关闭时将它们Dispose() It'll automatically proceed after everything is a happy walnut. 一切顺利之后,它将自动进行。 :) :)

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

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