Results 1 to 4 of 4

Thread: Stop my service through it's own code?

  1. #1

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Stop my service through it's own code?

    How does one stop a service through the code itself? I want to check for a valid license, and if it's expired, shut down the service.

    Calling this method in OnStart:
    VB Code:
    1. Private Sub CheckLicense()
    2.         EventLog1.WriteEntry("Your license has expired. Please contact support to obtain a new license.", EventLogEntryType.Error)
    3.         Logger.Log("I", Encoding.ASCII.GetBytes("Your license has expired. Please contact support to obtain a new license."))
    4.         'Application.Exit()
    5.         'OnStop()
    6.         Me.Dispose(True)
    7.     End Sub

    Application.Exit, OnStop and Me.Dispose do not shut it down.

    Thanks,
    Mike

  2. #2

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    FWIW, I've googled for hours and apparently, in this version, you just can't do this. That statement was from someone at Microsoft. The workaround suggested was to spawn another process which uses the ServiceController class to stop your service. I tried this within my service itself, and it does not work.

  3. #3
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    You could force the OnStart() sub to timeout by putting a massive sleep statement in there - the services control will report something like "[servicename] failed to start up - please see the eventlog for details".

  4. #4

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Thanks Merrion, that does the trick. Nice and simple, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width