如何确保 C# 控制台程序始终退出?

发布于 2024-12-15 17:12:04 字数 636 浏览 1 评论 0原文

我编写了一个小型 C# 控制台应用程序,供共享存储服务器上的许多用户使用。它的运行时间应该总是<大约 3 秒,并在后台自动运行,以协助用户真正尝试使用的另一个 GUI 应用程序。因此,我想确保程序始终完全退出,无论它是否抛出错误。

在Application_Startup中,我有以下基本结构:

try
{
    // Calls real code here
}
catch
{
    // Log any errors (and the logging itself has a try with empty catch around it
    // so that there's no way it can causes problems)
}
finally
{
    Application.Shutdown();
}

我认为有了这个结构,我的应用程序就不可能成为僵尸进程。然而,当尝试推送此应用程序的新版本时,我反复发现我无法删除和替换可执行文件,因为“文件正在使用中”,这意味着它挂在某人的计算机上,即使它应该只运行一段时间几秒钟并且总是关闭。

那么,为什么我的应用程序似乎成为人们计算机上具有我所拥有的代码结构的挂起进程呢?我缺少什么?

编辑:添加了“应用程序”。为了清楚起见,解析 ShutDown() 。

I've written a small C# console app that is used by many users on a shared storage server. It's runtime should always be < 3 seconds or so, and is run automatically in the background to assist another GUI app the user is really trying to use. Because of this, I want to make sure the program ALWAYS exits completely, no matter if it throws an error or what not.

In the Application_Startup, I have the basic structure of:

try
{
    // Calls real code here
}
catch
{
    // Log any errors (and the logging itself has a try with empty catch around it
    // so that there's no way it can causes problems)
}
finally
{
    Application.Shutdown();
}

I figured that with this structure, it was impossible for my app to become a zombie process. However, when trying to push new versions of this app, I repeatedly find that I cannot delete and replace the executable because the "file is in use", meaning that it's hanging on someone's computer out there, even though it should only run for a few seconds and always shutdown.

So, how is it that my app is seemingly becoming a hanging process on peoples' computers with the code structure I have? What am I missing?

Edit: Added "Application." to resolve ShutDown() for clarity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-12-22 17:12:04

这里有两个选项:

  1. 您的控制台应用程序实际上不会在 3 秒内完成,而是需要更长的时间。您需要对其进行调试,看看是什么花了这么长时间。
  2. 您的控制台应用程序需要 3 秒才能退出,但 GUI 每分钟都会运行一次,并且您有超过 40 个用户,因此找到未使用的可执行文件的可能性很小。

如果是第一个线程,并且您不想调试它,则可以随时启动第二个线程,等待 3 秒,然后终止整个进程。

There are two options here:

  1. Your console application doesn't really finish in 3 seconds, but rather takes a lot longer. You need to debug it and see what takes it that long.
  2. Your console application takes 3 seconds to exit, but it is run every minute by the GUI, and you have more than 40 users, so the probability of finding the executable unused are slim.

If it's the first one, and you don't want to debug it, you can always start a second thread, wait for 3 seconds and then kill the entire process.

<逆流佳人身旁 2024-12-22 17:12:04

也许 try 块内的代码仍在为至少一个客户端执行,并且实际上并不限于 3 秒左右。为了防止这种情况,您需要多线程应用程序 - 一个线程用于处理,另一个线程在后台在超时后杀死工作线程。在此之前,您应该问自己是否真的需要这样的基础设施。

我想到的另一件事是,其中一个用户当前正在运行该应用程序,概率取决于用户的数量。

也许将您的支持应用程序设计为始终运行的多线程服务会是一个更好的主意,而不是为每个客户端请求实例化一个正在运行的应用程序。

Maybe the code inside the try block is still executing for at least one of the clients and is not really limited to 3s or so. To prevent such case, you would need multithreaded application - one thread for processing and one in the background killing the working thread after a timeout. Prior to that you should ask yourself if such infrastructure is really needed.

Another thing that comes to mind would be that one of the users had the application running right at the moment, probability depends on the number of your users.

Maybe designing your support app as a always running multithreaded service would be a much better idea instead of instantiating one running application for each client request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文