Best practice to handle three services.
-
Hi all, I have a windows service, and I handle three events there like Timer, two port listeners. OnStop() of the service I want to kill/stop those three events. So, simply I catch the exception as follows.
protected override void OnStop() { try { TimerTicker.Stop(); LstOne.Stop(); LstTwo.Stop(); } catch(Exception ex) { EventLog.WriteEntry(ex.Source,ex.Message); } }
Ok, here is my question. Say the TimerTicker found an exception, OnStop() exit from there and next two event dead, I mean not stopped. How should I handle. Using separate three function to do this, seems to me odd. :(
I appreciate your help all the time... Eranga :)
-
Hi all, I have a windows service, and I handle three events there like Timer, two port listeners. OnStop() of the service I want to kill/stop those three events. So, simply I catch the exception as follows.
protected override void OnStop() { try { TimerTicker.Stop(); LstOne.Stop(); LstTwo.Stop(); } catch(Exception ex) { EventLog.WriteEntry(ex.Source,ex.Message); } }
Ok, here is my question. Say the TimerTicker found an exception, OnStop() exit from there and next two event dead, I mean not stopped. How should I handle. Using separate three function to do this, seems to me odd. :(
I appreciate your help all the time... Eranga :)
-
With Seprate try catch block inside one method.
Regards, Vythees Miles to go before sleep...
Thanks for the replay. Something like this.
try {
}
catch(correct_exception_for_timer) {
}
catch(correct_exception_for_listener) {
}
catch(correct_exception_for_listener_two) {
}Ok, say on the timer I got the exception. Is that remainders are execution. Like LstOne.Stop() is executed?
I appreciate your help all the time... CodingLover :)
-
Thanks for the replay. Something like this.
try {
}
catch(correct_exception_for_timer) {
}
catch(correct_exception_for_listener) {
}
catch(correct_exception_for_listener_two) {
}Ok, say on the timer I got the exception. Is that remainders are execution. Like LstOne.Stop() is executed?
I appreciate your help all the time... CodingLover :)
No. He means like this:
try
{
timer.Stop();
}
catch (Exception ex1)
{
//Do something with exception
}
try
{
FirstOne.Stop();
}
catch (Exception ex2)
{
//Do something with exception
}
try
{
SecondOne.Stop();
}
catch (Exception ex2)
{
//Do something with exception
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
No. He means like this:
try
{
timer.Stop();
}
catch (Exception ex1)
{
//Do something with exception
}
try
{
FirstOne.Stop();
}
catch (Exception ex2)
{
//Do something with exception
}
try
{
SecondOne.Stop();
}
catch (Exception ex2)
{
//Do something with exception
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Ok, I got the point. Is that the much better way to do it? :)
I appreciate your help all the time... CodingLover :)
-
Ok, I got the point. Is that the much better way to do it? :)
I appreciate your help all the time... CodingLover :)
It's the *only* way to do it if you want to try stopping all of the objects regardless of whether or not one or more of the others stop.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Ok, I got the point. Is that the much better way to do it? :)
I appreciate your help all the time... CodingLover :)
Write seprate methods like this..
public void StopTimer(bool suppressEx) { try { timer.Stop(); } catch (Exception ex) { //Do something with exception if( suppressEx == false) throw ex; }
In your Stop method you can call like this... public void Stop() { StopTimer(false); StopFirstPort(false); ...... } Choose the way you like...Regards, Vythees Miles to go before sleep...
-
Hi all, I have a windows service, and I handle three events there like Timer, two port listeners. OnStop() of the service I want to kill/stop those three events. So, simply I catch the exception as follows.
protected override void OnStop() { try { TimerTicker.Stop(); LstOne.Stop(); LstTwo.Stop(); } catch(Exception ex) { EventLog.WriteEntry(ex.Source,ex.Message); } }
Ok, here is my question. Say the TimerTicker found an exception, OnStop() exit from there and next two event dead, I mean not stopped. How should I handle. Using separate three function to do this, seems to me odd. :(
I appreciate your help all the time... Eranga :)
You can wrap each method in a delegate, pack the delegates in an array and then loop over the array. Something like
Action [] stopActions = { TimerTicker.Stop, LstOne.Stop, LstTwo.Stop };
foreach(Action action in stopActions)
{
try
{
action();
}
catch(Exception ex)
{
EventLog.WriteEntry(ex.Source, ex.Message);
}
}Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
It's the *only* way to do it if you want to try stopping all of the objects regardless of whether or not one or more of the others stop.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Logically yes, but the code can be made better by using a more functional approach.
Action [] stopActions = { TimerTicker.Stop, LstOne.Stop, LstTwo.Stop };
foreach(Action action in stopActions)
{
try
{
action();
}
catch(Exception ex)
{
EventLog.WriteEntry(ex.Source, ex.Message);
}
}Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Hi all, I have a windows service, and I handle three events there like Timer, two port listeners. OnStop() of the service I want to kill/stop those three events. So, simply I catch the exception as follows.
protected override void OnStop() { try { TimerTicker.Stop(); LstOne.Stop(); LstTwo.Stop(); } catch(Exception ex) { EventLog.WriteEntry(ex.Source,ex.Message); } }
Ok, here is my question. Say the TimerTicker found an exception, OnStop() exit from there and next two event dead, I mean not stopped. How should I handle. Using separate three function to do this, seems to me odd. :(
I appreciate your help all the time... Eranga :)
Thanks for all replays. :)
I appreciate your help all the time... CodingLover :)