永久运行的可执行文件
我使用 VS 2005 (c#) 创建了一个 winform(监控)应用程序,现在,当该应用程序因某种原因崩溃时我遇到问题,我必须确保它会自动重新启动。
我该如何解决这个问题? (也许通过使用 Windows 服务应用程序?)
谢谢
I created a winform (monitoring) application using VS 2005 (c#), and now, I have a problem when this application crashes for some reason, I have to be sure that it will be restarted automatically.
How can I resolve this? (maybe by using windows services application?)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,创建一个 Windows 服务 就可以了因为您可以将其设置为在崩溃时自动重新启动,但更好的方法是首先防止它崩溃!它崩溃有具体原因吗?
有了良好的错误处理和报告,您可以编写它,以便它简单地报告发生和继续的任何错误,恕我直言,这将是最好的路线
Yes, a creating a Windows Service would work as you can set it to automatically restart if it crashes but a better way would be to prevent it crashing in the first place! Is there a specific reason it crashes?
With good error handling and reporting you can write it so that it simply reports any errors that occur and carries on, which IMHO would be the best route to go
考虑一下:
http://msdn.microsoft.com/en-us/library/ cc303699.aspx
支持的最低服务器
Windows Server 2008
http ://msdn.microsoft.com/en-us/library/aa373347(VS.85).aspx
Consider this:
http://msdn.microsoft.com/en-us/library/cc303699.aspx
Minimum supported server
Windows Server 2008
http://msdn.microsoft.com/en-us/library/aa373347(VS.85).aspx
出于多种原因,对于任何长时间运行的后台进程来说,创建 Windows 服务都是一个非常好的主意,但是重新启动崩溃的应用程序不是其中之一!
您应该找出应用程序崩溃的原因,并且防止它发生。
无论如何,也将您的应用程序转换为 Windows 服务 - 您将看到许多好处,但是解决问题的正确方法是首先修复应用程序崩溃。
Creating a Windows service is a very good idea for any long-running background process for many reasons, however re-starting a crashed application is not one of them!
You should work out why the application is crashing and prevent it from happening.
By all means, also convert your application to a Windows service - you will see many benefits, however the correct way to solve your problem is to fix the application crash in the first place.
对于*强文本*观察者应用程序。
您应该在 Windows 服务上创建一个计时器,并在计时器滴答事件中编写类似这样的代码:
如果您真的无法对崩溃问题采取任何措施,我建议使用 try-catch 而不是观察者。 (不要忘记重新抛出已处理的主要异常)
For*strong text* a watcher app.
You should create a timer on the windows service and code something like this in the timer tick event:
if you really cant do anything about the crash problem i would recommend a try-catch instead of a watcher. (Dont forget to re-throw handled major exceptions)
由于您说您使用 Windows 窗体应用程序,因此您不能使用 Windows 服务,因为 Windows 服务不允许有 GUI。
我会做的是创建一个不可见的“看门狗”应用程序,它监视进程并在崩溃时自动重新启动它。
Since you say that you use a Windows Forms application you cannot use a Windows Service for that, since a Windows Service is not allowed to have a GUI.
What I would do it that I would create an invisible "watchdog" application which monitors the process and automatically restarts it when it crashes.
谢谢大家,我选择的解决方案是:在主程序中添加异常事件(UnhandledExceptionEventHandler 和 ThreadExceptionEventHandler 见上文),在这些事件中我重新启动程序(还放置日志和电子邮件来跟踪错误)。对于重新启动问题,我在 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] 中添加注册表项和我的应用程序路径,以确保我的应用程序将在 Windows 重新启动后重新启动;)
Thanks you all, the solution I choose is : in the main program I add an exception events (UnhandledExceptionEventHandler & ThreadExceptionEventHandler see above) in these events I restart the program (also putting log & email to trace errors). And for the reboot problem I add registry key in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] with my application path to be sure that my application will be restarted after the windows reboot ;)
您可以在最有可能导致崩溃的代码周围放置一个 try catch 块。然后将异常消息写入日志文件。您还可以在 catch 块中设置调试点以查看其他详细信息,例如调用堆栈等。
You can put a try catch block around the code that is most likely causing the crash. Then write the exception message to a log file. You can also set a debug point in the catch block to see other details like call stack, etc.