1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Window Service Stopped

Discussion in 'Programming' started by gray d' newbie, Aug 4, 2008.

  1. #1
    Greetings All,

    This is my first time creating a Window Service and I am having this error message when I try to start my window service (currently known as Service1). Below is my code for Window Service and also the line which I suspected is the caused of the problem.

    Protected Overrides Sub OnStart(ByVal args() As String)
    Dim i As Integer
    i = Shell("\\202.186.196.128\c$\Inetpub\wwwroot\WebAdmin\Rewards\print.exe", vbNormalFocus)

    Dim fs As FileStream = New FileStream("c:\temp\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write)
    Dim m_streamWriter As StreamWriter = New StreamWriter(fs)
    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End)
    m_streamWriter.WriteLine("BLPrinting Service: Service Started at " & Date.Now & Constants.vbLf)
    m_streamWriter.Flush()
    m_streamWriter.Close()
    End Sub

    Protected Overrides Sub OnStop()
    Dim fs As FileStream = New FileStream("c:\temp\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write)
    Dim m_streamWriter As StreamWriter = New StreamWriter(fs)
    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End)
    m_streamWriter.WriteLine("BLPrinting Service: Service Stopped at " & Date.Now & Constants.vbLf)
    m_streamWriter.Flush()
    m_streamWriter.Close()
    End Sub

    Above are the START and STOP functions for my Window Service, which I got it from some online tutorial. Since my window service is intended to call an EXE from a specific folder, hence I added in the 2 new lines, which I bolded. When I try to start my service again, it pops me this message:

    The Service1 service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.

    For your information, my window service was working fine before I added in the 2 new lines of code. Maybe they way I'm supposed to call an EXE is incorrect. Please correct me if I'm wrong.

    Thank you very much.

    Best Regards
    Gray d' Newbie
     
    gray d' newbie, Aug 4, 2008 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    Does \\202.186.196.128\c$\Inetpub\wwwroot\WebAdmin\Rewards\print.exe works from command prompt ? if no how would it work from your program?
     
    it career, Aug 5, 2008 IP
  3. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Please see comments below:

    1. The SHELL command accepts three optional parameters. One optional parameter is Timeout. If Timeout is omitted the invocation of print.exe may hang and NEVER return control to the service.
    2. Verify the optional Style parameter value. vbNormalFocus will give the print.exe window focus and not return control if the window is accepting user input (i.e. message box, etc.)
    3. Consider moving the hard coded values to an external configuration file for portability. Also, why shell through a UNC path to access the print.exe utility? Open UNC shares pose security risks.

    Hope the above helps a bit, good luck.
     
    Social.Network, Aug 5, 2008 IP