|
-
May 27th, 2017, 01:35 PM
#1
Thread Starter
New Member
Windows Service, Using timer, VB.net
Hi everyone. I am kind of newbie when it comes to writing windows services but I'm pretty sure the problem I'm experiencing is not just a mistake in understanding.
I had set up a windows service and had it working, firing off a timer every 60 seconds and writing an entry into my EventLog. It appears as if the minute I place any other code into it and I mean any other code, the timer stops working and won't even write the log entry.
I would think if it were a problem with some piece of the code following it; that it would at least write that line and then error.
Anyway Here is what I'm doing. I'm hoping maybe someone who has gone through this could give me a little direction to kick start the issue. Thanks in advance.
Code:
Imports System.Runtime.InteropServices
Imports WinSCP
Imports System.Threading
Public Class MyNewService
Public eventID As Integer
Declare Auto Function SetServiceStatus Lib "advapi32.dll" (ByVal handle As IntPtr, ByRef serviceStatus As ServiceStatus) As Boolean
Public Sub New()
MyBase.New()
' This call is required by the designer.
InitializeComponent()
Try
' Add any initialization after the InitializeComponent() call.
Me.EventLog1 = New System.Diagnostics.EventLog
If Not System.Diagnostics.EventLog.SourceExists("MyNewService") Then
System.Diagnostics.EventLog.CreateEventSource("MyNewService", "MyNewLog")
End If
EventLog1.Source = "MyNewService"
EventLog1.Log = "MyNewLog"
Catch err As System.Diagnostics.Eventing.Reader.EventLogException
MsgBox(err.InnerException)
End Try
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
Dim serviceStatus As ServiceStatus = New ServiceStatus()
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING
serviceStatus.dwWaitHint = 100000
SetServiceStatus(Me.ServiceHandle, serviceStatus)
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING
SetServiceStatus(Me.ServiceHandle, serviceStatus)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog1.WriteEntry("Service Started")
Dim timer As System.Timers.Timer = New System.Timers.Timer()
timer.Enabled = True
timer.Interval = 60000
AddHandler timer.Elapsed, AddressOf Me.OnTimer
timer.Start()
End Sub
Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
' TODO: Insert Monitoring Activies Here.
Try
EventLog1.WriteEntry("Checking For New Files", EventLogEntryType.Information, eventID)
' Setup session options
Dim sessionOptions As New SessionOptions
EventLog1.WriteEntry("SessionOptions", EventLogEntryType.Information, eventID)
With sessionOptions
.Protocol = Protocol.Sftp
.HostName = "XXXXXXXXXXX"
.UserName = "XXXXXXXXXXX"
.Password = "XXXXXXXXXXX"
.SshHostKeyFingerprint = "XXXXXXXXXXXXXXXX"
End With
Using session As New Session
' Connect
session.Open(sessionOptions)
EventLog1.WriteEntry("Session To Cheyney FTP Open")
' Download files
Dim transferOptions As New TransferOptions
transferOptions.TransferMode = TransferMode.Binary
Dim transferResult As TransferOperationResult
transferResult = session.GetFiles("/Inbox1/*", "C:\Localfolder\", False, transferOptions)
' Throw on any error
transferResult.Check()
' Print results
For Each transfer In transferResult.Transfers
EventLog1.WriteEntry(".....Transferring File:" & transfer.filename)
Next
End Using
Catch err As Exception
EventLog1.WriteEntry("Service Exception: " & err.InnerException.ToString)
End Try
eventID = eventID + 1
End Sub
Protected Overrides Sub OnStop()
Dim serviceStatus As ServiceStatus = New ServiceStatus()
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED
SetServiceStatus(Me.ServiceHandle, serviceStatus)
' Add code here to perform any tear-down necessary to stop your service.
EventLog1.WriteEntry("Service Stopped")
End Sub
Public Enum ServiceState
SERVICE_STOPPED = 1
SERVICE_START_PENDING = 2
SERVICE_STOP_PENDING = 3
SERVICE_RUNNING = 4
SERVICE_CONTINUE_PENDING = 5
SERVICE_PAUSE_PENDING = 6
SERVICE_PAUSED = 7
End Enum
<StructLayout(LayoutKind.Sequential)>
Public Structure ServiceStatus
Public dwServiceType As Long
Public dwCurrentState As ServiceState
Public dwControlsAccepted As Long
Public dwWin32ExitCode As Long
Public dwServiceSpecificExitCode As Long
Public dwCheckPoint As Long
Public dwWaitHint As Long
End Structure
End Class
Last edited by Shaggy Hiker; May 28th, 2017 at 10:45 AM.
Reason: Added CODE tags.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|