处理.net服务中的未处理异常 [英] Handling unhandled exception in .net service

查看:60
本文介绍了处理.net服务中的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我使用以下代码处理Windows窗体中的未处理异常。


Shared Sub Main()


Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException,AddressOf

GlobalExceptionHandler.OnThreadException

>
Application.Run(New MainForm)


End Sub


异常类:


公共类GlobalExceptionHandler


''处理异常事件


Public Sub OnThreadException(ByVal sender As Object,ByVal t As

Threading.ThreadExceptionEventArgs)


''日志异常


Helper.LogException(t.Exception," Unhandled exception 。


结束班级


开发Windows时可用什么代码处理未处理的异常

服务?


谢谢,


笔D.
解决方案

" Penelope Dramas" < penelopeDOTdramasATpro-transportDOTcomwrote in message

news:e%**************** @ TK2MSFTNGP02.phx.gbl ...


您好,


我使用以下代码处理Windows窗体中未处理的异常。


共享Sub Main()

Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException,AddressOf

GlobalExceptionHandler.OnThreadException


Application.Run(New MainForm)


End Sub


异常类:


公共类GlobalExceptionHandler


''处理异常事件


Public Sub OnThreadException(ByVal sender As对象,ByVal t As

Threading.ThreadExceptionEventArgs)


''日志异常


Helper.LogException(t .Exception,Unhandled exception。


结束班级


Wha在开发

Windows服务时,我可以使用t代码处理未处理的异常吗?



您向System.AppDomain.CurrentDomain添加处理程序。 UnhandledException

而不是Application.ThreadException


6月25日下午4:15,Penelope Dramas < penelopeDOTdramasATpro-

transportDOTcomwrote:


您好,


我使用以下代码在Windows窗体中处理未处理的异常。


共享子主体()


Dim GlobalExceptionHandler作为新的GlobalExceptionHandler


AddHandler Application.ThreadException,AddressOf

GlobalExceptionHandler.OnThreadException


Application.Run(New MainForm)


结束子


异常类:


公共类GlobalExceptionHandler


''处理异常活动


Public Sub OnThreadException(ByVal sender As Object,ByVal t As

Threading.ThreadExceptionEventArgs)


' 'log exception


Helper.LogException(t.Exception,Unhandled exception。)


End Class


我可以使用什么代码来处理开发时的未处理异常ping一个窗口

服务?


谢谢,


笔D.



最好不要在Windows服务中使用全局异常处理程序。

由于服务的性质,任何全局处理程序都可能无法处理

正确异常,服务可能会在您通知异常后通知

。最好将open catch块放在你需要它们的所有线程的基础上。你可以创建一个静态的

异常处理类来帮助你解决这个问题。


如果你还想使用全局处理程序,那么你需要附加到

当前域的UnhandledException事件。


AddHandler System.AppDomain.CurrentDomain.UnhandledException,

AddressOf GlobalExceptionHandler.OnUnhandledException


Penelope Dramas写道:


Shared Sub Main()

Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException,AddressOf

GlobalExceptionHandler.OnThreadException

Application.Run(New MainForm)

End Sub



" Global" (Eeek!)像这样的异常处理程序应被视为

后退;一种在你的应用程序之前记录终端错误的方法

进程终于过去并且死了 - 那个经典的卡通场景,其中一个

字符在悬崖的边缘跑了但是还没有还没开始下降。


不要指望他们能做更有用的事情。


在开发Windows

服务时,我可以用什么代码来处理未处理的异常?



奇怪的是,Windows服务比Windows Forms应用程序更简单,

所以放置一个Try .. Catch块包裹在主要的处理

方法为您的服务提供了解决方法(除非您使用计时器,否则我不建议使用
)。


当然,你需要它们在任何其他

线程的入口点周围,你可能会在服务中引发 - 这是尤其重要,

其中未处理的线程中的异常会终止整个过程(与

1.1不同,线程刚刚消失,留下应用程序的其余部分

想知道它去了哪里。)


HTH,

Phill W 。


Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException, AddressOf
GlobalExceptionHandler.OnThreadException

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

''Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

''log exception

Helper.LogException(t.Exception, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a Windows
Service?

Thanks,

Pen D.

解决方案

"Penelope Dramas" <penelopeDOTdramasATpro-transportDOTcomwrote in message
news:e%****************@TK2MSFTNGP02.phx.gbl...

Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException, AddressOf
GlobalExceptionHandler.OnThreadException

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

''Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

''log exception

Helper.LogException(t.Exception, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a
Windows Service?

You add a handler to System.AppDomain.CurrentDomain.UnhandledException
instead of Application.ThreadException


On Jun 25, 4:15 pm, "Penelope Dramas" <penelopeDOTdramasATpro-
transportDOTcomwrote:

Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalExceptionHandler As New GlobalExceptionHandler

AddHandler Application.ThreadException, AddressOf
GlobalExceptionHandler.OnThreadException

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

''Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

''log exception

Helper.LogException(t.Exception, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a Windows
Service?

Thanks,

Pen D.

It is best not to use global exception handlers in Windows Services.
Due to the nature of a service any global handler may not handle the
exception correctly and the service might terminate before you are
notified of the exception. It is better to put open catch blocks where
you need them and at the base of all threads. You can create a static
exception handling class that will help you with this.

If you still want to use a global handler then you need to attach to
the UnhandledException event of the CurrentDomain.

AddHandler System.AppDomain.CurrentDomain.UnhandledException,
AddressOf GlobalExceptionHandler.OnUnhandledException


Penelope Dramas wrote:

Shared Sub Main()
Dim GlobalExceptionHandler As New GlobalExceptionHandler
AddHandler Application.ThreadException, AddressOf
GlobalExceptionHandler.OnThreadException
Application.Run(New MainForm)
End Sub

"Global" (Eeek!) Exception handlers like these should be regarded as a
back-stop; a way to log an terminal error just before your application
process finally keels over and dies - that classic cartoon scene where a
character runs off the edge of a cliff but hasn''t started falling yet.

Don''t expect them to be able to do anything more useful.

What code can I use to handle unhandled exception when developing a Windows
Service?

Windows Services, oddly, are far simpler beasts than Windows Forms apps,
so placing a Try .. Catch block wrapped around the main "Processing"
method for your Service will do the trick (unless you use a Timer, which
I don''t recommend).

And, of course, you need them around the entry point(s) to any other
Thread(s) that you might spark off from within the Service - this is
particularly important if you''re heading into Framework 2.0 and beyond,
where unhandled Exceptions in a Thread kill the entire process (unlike
1.1, where the thread just disappears, leaving the rest of the app
wondering where it went).

HTH,
Phill W.


这篇关于处理.net服务中的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆