Members
Technology Zones
IBM Learning Center
Articles
Hosted By
Info
|
[3441] Creating a Windows Service in VB.NET
Last post 05-23-2008 10:35 AM by mekaushik. 102 replies.
-
-
soulliam


- Joined on 07-15-2005

- Points 15
|
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
 
|
|
Advertisement
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
Search
Code Samples
New Members
|