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. When to call base.Onstart

When to call base.Onstart

Scheduled Pinned Locked Moved C#
question
6 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.
  • J Offline
    J Offline
    Jordanwb
    wrote on last edited by
    #1

    I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?

    protected override void OnStart(string[] args)
    {
    // Do Stuff
    base.OnStart(args);
    }

    Or:

    protected override void OnStart(string[] args)
    {
    base.OnStart(args);
    // Do Stuff
    }

    D P 2 Replies Last reply
    0
    • J Jordanwb

      I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?

      protected override void OnStart(string[] args)
      {
      // Do Stuff
      base.OnStart(args);
      }

      Or:

      protected override void OnStart(string[] args)
      {
      base.OnStart(args);
      // Do Stuff
      }

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Normally OnXxx methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      L J 2 Replies Last reply
      0
      • D DaveyM69

        Normally OnXxx methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        DaveyM69 wrote:

        If it doesn't matter then it doesn't matter!

        when you're right, you're right indeed! :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        D 1 Reply Last reply
        0
        • L Luc Pattyn

          DaveyM69 wrote:

          If it doesn't matter then it doesn't matter!

          when you're right, you're right indeed! :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          :laugh:

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          1 Reply Last reply
          0
          • J Jordanwb

            I have a class that inherits System.ServiceProcess.ServiceBase and is overriding the OnStart (string[] args) method. Where do I call base.OnStart(args)? At the beginning of the method or at the end?

            protected override void OnStart(string[] args)
            {
            // Do Stuff
            base.OnStart(args);
            }

            Or:

            protected override void OnStart(string[] args)
            {
            base.OnStart(args);
            // Do Stuff
            }

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            That depends on what else you do in your OnStart; I call base.OnStart in the middle:

                protected override void
                OnStart
                (
                    string\[\] args
                )
                {
            

            if DEBUG

                    System.Threading.Thread.Sleep ( 60000 ) ;
            

            endif

                    this.oktoproceed = true ;
            
                    base.OnStart ( args ) ;
            
                    if ( this.when != null )
                    {            
                        this.when.Start() ;
                    }
                    
                    LogMessage ( this.ServiceName + " started" ) ;
                    
                    return ;
                }
            

            (The Sleep gives me time to attach the debugger. when is a System.Timers.Timer.)

            1 Reply Last reply
            0
            • D DaveyM69

              Normally OnXxx methods are used for raising events (and occaisionaly other logic that relates to an action but that should be documented). If your code in _Do stuff_relies upon this event already being raised (or any other logic that might be in the base method being applied) then you should call it first. If it's important that your Do stuff is done before any events are raised etc then you should call it afterwards. If it doesn't matter then it doesn't matter!

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              J Offline
              J Offline
              Jordanwb
              wrote on last edited by
              #6

              Sorry for not getting back sooner. In the OnStart method I'll be creating a couple of threads, a dozen or sockets and creating some objects but they won't depend on base.OnStart(). Let's say that something in my OnStart fails (file not found for example), how would I report that startup has failed and stop the startup of the service? Do I simply not call base.OnStart()?

              modified on Sunday, December 6, 2009 9:15 PM

              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