473,411 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Windows Service not looping

I have built a windows service that should check whether a page can be returned from a site (from various servers). If not, it logs to the Application Error log. My trouble is this. It runs once, but doesn't seem to loop at all, ie, I only get the 1 round of warning messages (it also sends me a net message, but that will be removed eventually). My code for the whole service is below:

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Net
  4. Imports System.Text
  5. Imports System.Net.Dns
  6. Imports System.Threading
  7. Imports System.ServiceProcess
  8.  
  9. Public Class IISPolling
  10.     Inherits System.ServiceProcess.ServiceBase
  11.  
  12.     Private thrPollingThread As New Thread( _
  13.         New ThreadStart(AddressOf PollProcess))
  14.  
  15.     Dim arrServers(2, 1) As String
  16.     Dim x, y As Integer
  17.  
  18.     Const iChunk = 512
  19.     Dim arrRecipients() As String
  20.     Dim strLocalName As String = GetHostName()
  21.     Dim strServerName As String
  22.     Dim strMsg As String
  23.     Dim lngPos As Long = 1
  24.  
  25. #Region " Component Designer generated code "
  26.  
  27.     Public Sub New()
  28.         MyBase.New()
  29.  
  30.         ' This call is required by the Component Designer.
  31.         InitializeComponent()
  32.  
  33.  
  34.     End Sub
  35.  
  36.     'UserService overrides dispose to clean up the component list.
  37.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  38.         If disposing Then
  39.             If Not (components Is Nothing) Then
  40.                 components.Dispose()
  41.             End If
  42.         End If
  43.         MyBase.Dispose(disposing)
  44.     End Sub
  45.  
  46.     ' The main entry point for the process
  47.     <MTAThread()> _
  48.     Shared Sub Main()
  49.         Dim ServicesToRun() As System.ServiceProcess.ServiceBase
  50.  
  51.         ServicesToRun = New System.ServiceProcess.ServiceBase() {New IISPolling}
  52.  
  53.         System.ServiceProcess.ServiceBase.Run(ServicesToRun)
  54.     End Sub
  55.  
  56.     'Required by the Component Designer
  57.     Private components As System.ComponentModel.IContainer
  58.  
  59.     ' NOTE: The following procedure is required by the Component Designer
  60.     ' It can be modified using the Component Designer.  
  61.     ' Do not modify it using the code editor.
  62.     Friend WithEvents logEvents As System.Diagnostics.EventLog
  63.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  64.         Me.logEvents = New System.Diagnostics.EventLog
  65.         CType(Me.logEvents, System.ComponentModel.ISupportInitialize).BeginInit()
  66.         '
  67.         'logEvents
  68.         '
  69.         Me.logEvents.EnableRaisingEvents = True
  70.         Me.logEvents.Log = "Application"
  71.         Me.logEvents.Source = "IISPolling"
  72.         '
  73.         'IISPolling
  74.         '
  75.         Me.ServiceName = "IISPolling"
  76.         CType(Me.logEvents, System.ComponentModel.ISupportInitialize).EndInit()
  77.  
  78.     End Sub
  79.  
  80. #End Region
  81.  
  82.     Protected Overrides Sub OnStart(ByVal args() As String)
  83.         ' Add code here to start your service. This method should set things
  84.         ' in motion so your service can do its work.
  85.         logEvents.WriteEntry("IIS Polling service started on " & strLocalName & ".")
  86.  
  87.         ' Start the thread.
  88.         thrPollingThread.Start()
  89.     End Sub
  90.  
  91.     Protected Overrides Sub OnStop()
  92.         ' Add code here to perform any tear-down necessary to stop your service.
  93.         logEvents.WriteEntry("IIS Polling service stopped on " & strLocalName & ".")
  94.  
  95.         ' Stop the thread.
  96.         thrPollingThread.Abort()
  97.     End Sub
  98.  
  99.     Private Sub PollProcess()
  100.         Do
  101.             ' Wait...
  102.             Thread.Sleep(30000)
  103.             PollingPass()
  104.         Loop
  105.     End Sub
  106.  
  107.     Private Sub PollingPass()
  108.  
  109.         Dim request As HttpWebRequest
  110.         Dim response As HttpWebResponse
  111.  
  112.         arrServers(0, 0) = "http://server1/DotNetNuke/Default.aspx"
  113.         arrServers(0, 1) = "serverA"
  114.         arrServers(1, 0) = "http://server2/DotNetNuke/Default.aspx"
  115.         arrServers(1, 1) = "serverB"
  116.         arrServers(2, 0)  = "http://server3/DotNetNuke/Default.aspx"
  117.         arrServers(2, 1) = "serverC"
  118.  
  119.         x = 0
  120.         Do Until x > arrServers.Length - 1
  121.  
  122.             strServerName = arrServers(x, 1)
  123.             request = CType(WebRequest.Create(arrServers(x, 0)), HttpWebRequest)
  124.  
  125.             'set some reasonable limits on resources used by this request
  126.             request.MaximumAutomaticRedirections = 4
  127.             request.MaximumResponseHeadersLength = 4
  128.  
  129.             'set credentials to use for this request.
  130.             request.Credentials = CredentialCache.DefaultCredentials
  131.  
  132.             Try
  133.                 response = CType(request.GetResponse(), HttpWebResponse)
  134.                 'TODO: take this line out otherwise the event log will just fill up
  135.                 'logEvents.WriteEntry("IIS running on " & strServerName)
  136.             Catch ex As Exception
  137.                 logEvents.WriteEntry("IIS on " & strServerName & " is not responding '" & _
  138.                     ex.Message & "'", EventLogEntryType.Warning)
  139.                 logEvents.WriteEntry("IIS service on " & strServerName & " - Stack Trace: " & _
  140.                     ex.StackTrace, EventLogEntryType.Warning)
  141.  
  142.                 SendNetMsg("IIS Is Not Responding on server: " & strServerName)    'send a net message
  143.  
  144.             End Try
  145.             x += 1
  146.         Loop
  147.         If Not response Is Nothing Then
  148.             response.Close()
  149.         End If
  150.  
  151.     End Sub
  152.  
  153.     Private Sub SendNetMsg(ByVal msg As String)
  154.  
  155.         arrRecipients(0) = "myPC"
  156.         'arrRecipients(1) = "myPC2"
  157.         y = 0
  158.  
  159.         Do Until lngPos > Len(strMsg)
  160.             Do Until y > arrRecipients.Length - 1
  161.                 Dim lretval As Long = Shell("net send " & arrRecipients(y) & _
  162.            " """ & Mid(msg, lngPos, iChunk) & """", vbHide)
  163.  
  164.                 lngPos += iChunk
  165.                 'pause to stop out of sequence message chunks
  166.                 Thread.Sleep(100)
  167.                 y += 1
  168.             Loop
  169.         Loop
  170.  
  171.     End Sub
  172.  
  173. End Class
Mar 20 '07 #1
1 1325
kenobewan
4,871 Expert 4TB
My assumption is the exception is caught, sendnetmsg is called and runs only once as y=0 & arrRecipients.Length - 1=0. Therefore y only has to run once to be greater than 0.

Otherwise you will need to do some debugging - I would parse the code down until it is working and build it back up. HTH.
Mar 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Fabio Papa | last post by:
I am trying to write a windows service that sends emails to clients at specific times based on information in a sql db. Since this is done for multiple cities, I start a thread for each city and...
8
by: doug.h.taylor.accipitersoftware.com | last post by:
Hi, I need a general direction. I don't even know where to start researching how to do this. I have an existing dot net web application (It's mine, I wrote it) that has some text fields and...
6
by: NWx | last post by:
Hi, I have an ASP.NET application which allow registered user to set-up notifications, saved into an SQL server database. I want to make a win service which periodically check this SQL server...
3
by: James Dixon | last post by:
I have created a windows service in C#, .net framework 1.1 The service makes a web request using the mshtml.HTMLDocument.CreateDocumentFromURL() function Because this is not using...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
2
by: Dima Protchenko | last post by:
Hi, I am building a windows service, which monitors a mailbox and parses email messages into the db. When I wrote the SAME code in windows forms application, everything worked fine, but I am having...
5
by: Steven Thomas | last post by:
I am writing an window service application in vb.net. I have a sub that when called will start looping and doing work as long as the service is running. The problem I have is when I call the...
6
by: shil | last post by:
Hi, I am writing a windows app in .net 2003. I have a datagrid which gets data from a storedprocedure. My question is how can I update the data in the datagrid? I want to call another...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.