473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Service not looping

15 New Member
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 1340
kenobewan
4,871 Recognized Expert Specialist
My assumption is the exception is caught, sendnetmsg is called and runs only once as y=0 & arrRecipients.L ength - 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
2022
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 continue the processing from each thread. My service starts fine (gives me no errors, etc), but it doesn't seem to start the new threads. I am new...
8
5849
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 a button. The code-behind click event of the button sends an SNPP conversation to a pager server. I also have an existing dot net windows service...
6
340
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 table, and send e-mail at the moment a notification is scheduled. Question: what is the best way to implement this? Should I create a timer, and in...
3
9402
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 Windows.Forms, I can't use the Application.DoEvents() function while the request is completing. Is there an equivilent function (call the CreateDocumentFromURL...
2
13178
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 System.Timers.Timer but i had to remove it because of known bug(842739). Now i am using System.Threading.Timer. It executes routine fine but the...
2
1196
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 a problem in the Windows Service project and I can't figure it out. For i As Integer = objFolder.Items.Count To 1 Step -1 If TypeOf...
5
5010
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 sub from the onstart sub the service never thinks it has started. It remains in a "Starting" state. Therefore I can not stop the service either. ...
6
4669
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 storedprocedure to update the data in the datagrid. Thanks in advance.
4
4173
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 check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.