Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[3441] Creating a Windows Service in VB.NET

Last post 05-23-2008 10:35 AM by mekaushik. 102 replies.
Page 5 of 7 (103 items) « First ... < Previous 3 4 5 6 7 Next >
Sort Posts: Previous Next
  • 07-15-2005 4:04 PM In reply to

    • soulliam
    • Not Ranked
    • Joined on 07-15-2005
    • New Member
    • Points 15

    Threads in a service

    I have written a few services with no major problems, until this last one.  
    When I start this current service I get the message "service started and then stopped".
    The only difference I can tell between this service and another service I wrote is the
    use of threads.  Is there any obvious problem here?



    Imports System.ServiceProcess
    Imports System.Xml
    Imports System.Threading
    Imports System.net

    Public Class Service1
       Inherits System.ServiceProcess.ServiceBase
       Dim initialized As Boolean
       Dim Sent As Boolean
       Dim EMail_From, EMail_To, cc1, Subject, body, SiteUrl, SiteName, Search As String
       Dim done As Boolean
       Dim page As Thread
       Dim reset As Thread
       Dim timeout As Thread
       Dim failedsite(0)
       Dim timecount As Integer


    #Region " Component Designer generated code "

       Public Sub New()
           MyBase.New()

           ' This call is required by the Component Designer.
           InitializeComponent()

           ' Add any initialization after the InitializeComponent() call

       End Sub

       'UserService overrides dispose to clean up the component list.
       Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
           If disposing Then
               If Not (components Is Nothing) Then
                   components.Dispose()
               End If
           End If
           MyBase.Dispose(disposing)
       End Sub

       ' The main entry point for the process
       <MTAThread()> _
       Shared Sub Main()
           Dim ServicesToRun() As System.ServiceProcess.ServiceBase

           ' More than one NT Service may run within the same process. To add
           ' another service to this process, change the following line to
           ' create a second service object. For example,
           '
           '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
           '
           ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}

           System.ServiceProcess.ServiceBase.Run(ServicesToRun)

           

       End Sub

       'Required by the Component Designer
       Private components As System.ComponentModel.IContainer

       ' NOTE: The following procedure is required by the Component Designer
       ' It can be modified using the Component Designer.  
       ' Do not modify it using the code editor.
       Friend WithEvents Timer1 As System.Timers.Timer
       <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
           Me.Timer1 = New System.Timers.Timer
           CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
           '
           'Timer1
           '
           Me.Timer1.Enabled = True
           Me.Timer1.Interval = 15000
           '
           'Service1
           '
           Me.ServiceName = "Service1"
           CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()

       End Sub

    #End Region

       Protected Overrides Sub OnStart(ByVal args() As String)
           ' Add code here to start your service. This method should set things
           ' in motion so your service can do its work.
           
           timecount = 0
           MsgBox("Start")
       End Sub

       Protected Overrides Sub OnStop()
           ' Add code here to perform any tear-down necessary to stop your service.
       End Sub

       Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
             
           done = False
           timecount = timecount + 1
           page = New Thread(AddressOf Me.getInfo)
           timeout = New Thread(AddressOf Me.Expire)

           page.Start()
           timeout.Start()
           
           Call ResetFailure()

       End Sub

       Public Sub ResetFailure()
           Dim i As Integer
           If timecount >= 5 Then
               For i = 0 To failedsite.Length
                   failedsite(i) = ""
               Next
               failedsite.Clear(failedsite, 0, failedsite.Length)
               timecount = 0
           End If
       End Sub


       Public Sub Expire()
           Thread.CurrentThread.Sleep(60000)
           If done = False Then
               page.Abort()
               body = "Time out: "
           End If
       End Sub

       Public Sub getInfo()
           Dim reader As XmlTextReader
           Dim reader2 As XmlTextReader
           Dim i As Integer
           Dim found As Boolean


           reader = New XmlTextReader("C:\Init2.xml")
           reader2 = New XmlTextReader("C:\Init2.xml")
           While reader.Read()
               If reader.NodeType = XmlNodeType.Element Then
                   If reader.Name = "EMail_To" Then
                       EMail_To = reader.GetAttribute(0)
                   ElseIf reader.Name = "cc1" Then
                       cc1 = reader.GetAttribute(0)
                   ElseIf reader.Name = "EMail_From" Then
                       EMail_From = reader.GetAttribute(0)
                   ElseIf reader.Name = "SiteUrl" Then
                       SiteUrl = reader.GetAttribute(0)
                   ElseIf reader.Name = "SiteName" Then
       
    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 07-18-2005 4:53 PM In reply to

    Windows Service UI

    I also would like to create a client/UI that could communicate with my windows service.

    I am looking for an architecture similiar to SQL Server and the Enterprise Manager.  SQL Server is the Service and the Enterprise Manager is a management piece that interacts with the service, to display all available databases, allows for administration of the services configuration, etc.

    Any suggestions welcome,

    • Post Points: 0
  • 07-18-2005 8:27 PM In reply to

    • jaepetto
    • Not Ranked
    • Joined on 06-29-2005
    • New Member
    • Points 10
    Hi,

    I would certainly go the easy way.

    Within the service code, I would put an event handler on any modification on a given directory.
    Thus whenever a file is placed in that directory, the file is parsed by the service and an action can be taken.
    Concerning the file format, I would use an XML file. It's easier to parse and to structure.

    A second possibility is to use a database with a table containing the actions to perform by the service.
    The client writes an entry in the table, the service checks the table on a schedule basis (e.g. a timer event) and do the action.

    A third possibility would be to use a socket.

    It's really up to you.
    • Post Points: 0
  • 07-26-2005 1:25 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80

    not able to see events on timer_tick

    I could buid the windows service with the steps mentioned but noticed that the event is not logged on Timer_Tick event. The event is logged only on the start and stop of the windows service.
    And when I attached the debugger with the service from .NET IDE, it never stepped into the Timer_Tick routine.
    Please help me to debug the service and let me know how we can log the event on timer click.

    thanks
    vanj
    • Post Points: 0
  • 07-26-2005 2:35 PM In reply to

    • soulliam
    • Not Ranked
    • Joined on 07-15-2005
    • New Member
    • Points 15

    timer

    Are you using the component timer and not the windows forms timer?  When creating a Service you should use the component objects.
    • Post Points: 0
  • 07-26-2005 3:03 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    Yes soulliam,
    I have added the componets timer.

    I wrote the code to write log on to the text file. I am trying to write "Another entry" under the Timer_Tick sub but this message is never written on the text file.

    Find the code below.
    See if you can find anything wrong with it.

    Thanks,
    vanj.

       Protected Overrides Sub OnStart(ByVal args() As String)
           ' Add code here to start your service. This method should set things
           ' in motion so your service can do its work.
           LogMessge("Stariting the service")
           Timer1.Enabled = True
           Timer1.Start()
       End Sub

       Protected Overrides Sub OnStop()
           ' Add code here to perform any tear-down necessary to stop your service.
           LogMessge("Stop Service")
           Timer1.Stop()
       End Sub

       Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
           LogMessge("Another entry")
       End Sub

       Private Sub LogMessge(ByVal messge As String)

           Dim objFileStream As FileStream
           Dim objStringBuilder As StringBuilder = New StringBuilder

           ' Append the message
           objStringBuilder.AppendFormat("{0}{1}", messge, Environment.NewLine)

           If (Not Directory.Exists(Path.GetDirectoryName("D:\Testing\MyService\WinServ.txt"))) Then
               Directory.CreateDirectory(Path.GetDirectoryName("D:\Testing\MyService\WinServ.txt"))
           End If

           'Check if the file already exists
           'If file already exists open file in append mode
           'else create file
           If (File.Exists("D:\Testing\MyService\WinServ.txt")) Then
               objFileStream = File.Open("D:\Testing\MyService\WinServ.txt", FileMode.Append, FileAccess.Write)
           Else
               objFileStream = File.Create("D:\Testing\MyService\WinServ.txt")
           End If

           Dim objStreamWriter As New StreamWriter(objFileStream)

           objStreamWriter.Write(objStringBuilder.ToString())
           objStreamWriter.Close()
       End Sub
    End Class
    • Post Points: 0
  • 07-26-2005 3:10 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80

    [Resolved]

    Hi soulliam
     I created the project again with component timer and under timer_elapsed handler have the required code and it works fine.

    thanks
    vanj
    • Post Points: 0
  • 07-26-2005 3:18 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80

    How to create an interface to start or stop the se

    Hi all,

    I would like to create a small UI associated with the service running. So that the user need
    not go to settings-->ControlPanel-->AdminstativeTools-->Services to start or stop a service.

    I want to have something similar to SQL server, which sits in the taskbar and user can start and stop the SQLserver from there.

    Please let me know how I can achieve something similar

    regards,
    vanj.
    • Post Points: 0
  • 07-26-2005 3:24 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    I think the problem is due to :
    MsgBox("Start") under OnStart routine.

    Comment this line of the code and try it should work fine

    good luck!
    vanj.
    • Post Points: 0
  • 07-29-2005 8:15 PM In reply to

    Creating a client for a Windows Service

    I've been trying to figure out the best way to have a client connect to a Windows Service. Would that be through Remoting or is there another way it can be done? Has anyone tried this?
    • Post Points: 0
  • 07-30-2005 5:46 AM In reply to

    • MoJo
    • Not Ranked
    • Joined on 07-30-2005
    • New Member
    • Points 5

    Start a WinForm App from a Service

    Is there any way to start a WinForm App from a VB .Net Service?
    • Post Points: 0
  • 08-03-2005 3:46 AM In reply to

    If you are programming in .NET, then take alook at the System.ServiceProcess.ServiceController class. It has methods which let you start, stop, pause, query and modify windows services.

    Must say that I've only just started in on this myself today with the kick off of a new project.
    • Post Points: 0
  • 08-03-2005 11:31 AM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80

    Deploying windows service

    Hi all,
    On the machine which has dot net IDE we use
    the .NET command prompt and run InstallUtil ExeName.exe to make it a windows service.

    But when I want to deploy the application on another machine which has only the DOT NET FRAMEWORK and no IDE, how do I do it.

    Regards,
    vanj.
    • Post Points: 0
  • 08-03-2005 12:18 PM In reply to

    • vanj
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80

    [Resolved]

    As Dot Net frame work has InstallUtil.exe, in the command prompt go to the path where the InstallUtil.exe is present on the machine and run the command --InstallUtil Exename.exe, it will register the windows service
    • Post Points: 10
  • 08-08-2005 11:36 PM In reply to

    • brandoe1
    • Not Ranked
    • Joined on 08-08-2005
    • New Member
    • Points 10

    Starting service after install

    Need Help!!

    I've created a vb.net windows service using vs.net 2003 and added a setup and deployment project to my solution to install the service.

    The installer works great, the service is installed, however I need to add the ability for the service to be automatically started after the installer is finished. I.E. - the service needs to be started without having to restart the computer or manually go into the service control panel and starting.

    Does anybody know how I can start the service from code thats inside my installer??

    Any help would be greatly appreciated!!
    • Post Points: 0
Page 5 of 7 (103 items) « First ... < Previous 3 4 5 6 7 Next >