Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Best practice to handle three services.

Best practice to handle three services.

Scheduled Pinned Locked Moved C#
helpquestiondiscussion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    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 :)

    V S C 3 Replies Last reply
    0
    • C CodingLover

      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 :)

      V Offline
      V Offline
      vytheese
      wrote on last edited by
      #2

      With Seprate try catch block inside one method.

      Regards, Vythees Miles to go before sleep...

      C 1 Reply Last reply
      0
      • V vytheese

        With Seprate try catch block inside one method.

        Regards, Vythees Miles to go before sleep...

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #3

        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 :)

        R 1 Reply Last reply
        0
        • C 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 :)

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • R realJSOP

            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

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            Ok, I got the point. Is that the much better way to do it? :)

            I appreciate your help all the time... CodingLover :)

            R V 2 Replies Last reply
            0
            • C CodingLover

              Ok, I got the point. Is that the much better way to do it? :)

              I appreciate your help all the time... CodingLover :)

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • C CodingLover

                Ok, I got the point. Is that the much better way to do it? :)

                I appreciate your help all the time... CodingLover :)

                V Offline
                V Offline
                vytheese
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0
                • C CodingLover

                  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 :)

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • R realJSOP

                    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

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    Logically 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

                    1 Reply Last reply
                    0
                    • C CodingLover

                      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 :)

                      C Offline
                      C Offline
                      CodingLover
                      wrote on last edited by
                      #10

                      Thanks for all replays. :)

                      I appreciate your help all the time... CodingLover :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups