【发布时间】:2010-08-06 15:27:28
【问题描述】:
我有一个用 C# 编写的进程,它没有任何形式或输出。
static void Main() {
MyBackgroundProcess process = new MyBackgroundProcess();
// begin background process
process.Start();
// keep application running in the background, run events
Application.Run();
}
我需要在应用程序终止时释放一些资源并做其他事情(这应该只在运行它的用户注销或通过任务管理器手动终止时;该进程永远不会自行终止)。
处理这个问题的最佳方法是什么?
我尝试过但没有奏效的方法:
- 将事件挂钩到 Application.ApplicationExit 和 Application.ThreadExit
- 让 MyBackgroundProcess 实现 IDisposable 并将 Main 逻辑包装在 using() 中
【问题讨论】:
标签: .net