c# - Windows Service Exception Handling and Timers -
I have a problem with System.Timers.Timer does not throw any exceptions in this here: My code is here: < / P>
Private Timer Mitimer {Receive; Set; } Public MyService () {InitializeComponent (); AppDomain.CurrentDomain.UnhandledException + currentDomain_UnhandledException; MyTimer = New Timer (10 * 1000); MyTimer.Elapsed + = MyTimer_Elapsed; } Safe Override Zero Onstart (string [] args) {MyTimer.Enabled = true; } Private Zero CurrentDomain_UnhandledException (Object Sender, UnhandledExceptionEventArgs e) {if Event! LoggerLock.Com } EventLog.WriteEntry (EVENTLOGSOURCE, "UnhandledException \ r \ n" + e.ExceptionObject, EventLogEntryType.Error); EventLog.WriteEntry (EVENTLOGSOURCE, "Unlocked Expressionergart.Etestimating:" + E.I.S.Termitting, Event LogENTER TypeError); If (EITTETING) {this.ExitCode = 100; //this.Stop (); }} Private Zero MyTimer_Elapsed (Object Sender, ElapsedEventArgs e) {MyTimer.Enabled = false; CurrentDomain_UnhandledException (AppDomain.CurrentDomain, New UnhandledExceptionEventArgs (New Exception ("FakeExceptionForTestPurposeOnly: It Will Be Logged!", False)); Int32 i = Convert ToInt32 ("10_ForSureGeneratesException_10"); }
Then, in my event log "FakeExceptionForTestPurposeOnly: it will be logged!" But this is only one!
After that, the bad part is the service that is still going on. How is this possible? Please note I have already solved this problem, in that case the use of the timer is not mandatory. threading. Thread trick and works as a clockwork. I'm just curious ...
The question is then: Why MyTimer_Elapsed () "CurrentDomain_UnhandledException (Object Sender, UnhandledExceptionEventArgs e ) Will never be removed during the event of any uncontrolled exception
Any help, suggestions, comments will be appreciated
< Div class = "post-text" itemprop = "text">
In a Windows service, the "main" thread is owned by service control manager
, Which calls onstart
and OnStop
methods. When an exception occurs on the "Main" thread, then Servicebase
is in charge of dealing with class exceptions, thus takes care of the exception and does not promote the call series.
Although there should be no synchronization context
inside a Windows service, hence the Elapsed
event is set on a separate ThreadPool
thread Should be removed, I can suggest instead, you are trying to do System.Threading.Timer
, and see that the exception spreads when Elapsed
events Calls callback.
Comments
Post a Comment