none
Issue with creating a windows service, TIMER and VB.net RRS feed

  • Question

  • Good morning.   I have been attempting to debug an issue for the last few days and I cannot seem to discover what it is.  It is a very simple windows service which works fine provided I only kick off the timer and in that routine only write to the eventlog.  That works great.  Every minute it writes the message... The minute I add any code to create an object, it fails.  However it does not catch an exception at all.  It also does not write the eventlog note either.  

    I can understand if there was a problem creating the object but I have no idea why the try catch isn't producing anything.

    I know I can do this in an exe that I put in the windows scheduler but I'd rather do it as a service, I simply can't identify what's going on.  Below is my exact code less login specifics.  Anything anyone can provide direction wise would be greatly appreciated..

    Imports System.Runtime.InteropServices
    Imports WinSCP
    Imports System.Threading
    
    Public Class MyNewService
        Public eventID As Integer
    
        'Public Shared stateTimer As Timer
        'Public Shared timerDelegate As TimerCallback
    
        Public timer As System.Timers.Timer
    
        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 oCallBack As New TimerCallback(AddressOf OnTimer)
            'oTimer = New System.Threading.Timer(oCallBack, Nothing, 300000, 300000)
            'EventLog.WriteEntry("CCFinalizeService has begun successfully.", _
            '                    System.Diagnostics.EventLogEntryType.Information)
            timer = New System.Timers.Timer()
            timer.Enabled = True
            timer.Interval = 60000
            AddHandler timer.Elapsed, AddressOf Me.OnTimer
            timer.Start()
    
    
    
    
            'timerDelegate = New TimerCallback(AddressOf OnTimer)
            'stateTimer = New Timer(timerDelegate, Nothing, 60000, 60000)
    
        End Sub
    
        Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
            ' TODO: Insert Monitoring Activies Here.
            Try
                EventLog1.WriteEntry("Checking For New PHEAA Files", EventLogEntryType.Information, eventID)
    
    
                ' Setup session options
                Dim sessionOptions As New WinSCP.SessionOptions
    
                With sessionOptions
                    .Protocol = Protocol.Sftp
                    .HostName = "XXXXXXXXXXXX"
                    .UserName = "XXXXXXXXXXXX"
                    .Password = "XXXXXXXXXXXX"
                    .SshHostKeyFingerprint = "XXXXXXXXXXXXXXXXXXXX"
                End With
    
                Using session As New Session
                    ' Connect
                    session.Open(sessionOptions)
                    EventLog1.WriteEntry("Session To FTP Open")
    
                    ' Download files
                    Dim transferOptions As New TransferOptions
                    transferOptions.TransferMode = TransferMode.Binary
    
                    Dim transferResult As TransferOperationResult
                    transferResult = session.GetFiles("/Inbox1/*", "C:\PATH\", 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 UInt64
            Public dwCurrentState As ServiceState
            Public dwControlsAccepted As UInt64
            Public dwWin32ExitCode As UInt64
            Public dwServiceSpecificExitCode As UInt64
            Public dwCheckPoint As UInt64
            Public dwWaitHint As UInt64
        End Structure
    
    End Class
    

    Tuesday, May 30, 2017 1:12 PM

All replies

  • The minute I add any code to create an object, it fails.  However it does not catch an exception at all.  It also does not write the eventlog note either.  

    I don't think anybody can help you with this information you give about the problem, maybe they try, but then they stop at the middle. If you want help than tell not any but show what code gives the trouble. 


    Success
    Cor

    Tuesday, May 30, 2017 1:27 PM
  • Hi Dan,

    What is WinSCP?  If it is this: https://winscp.net/eng/index.php then that's probably the issue...

    That library most likely relies on desktop resources which are not available to a Windows service.  While it is possible to flag a service to allow interaction with the desktop, this should generally be avoided and even when enabled, will not stop a service from failing if it requires a desktop and there is no user logged onto the computer.

    It may be that you simply cannot use that DLL in a service application.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Frank L. Smith Wednesday, May 31, 2017 1:18 PM
    Tuesday, May 30, 2017 2:00 PM
    Moderator
  • Hi Reed,

    I saw you wrote in the thread "Food for Thought" that there are MSFT moderators who think they always must interfere in questions. 

    I did not understand that you did mean "Only I may interfere wild all kind of wild guesses"




    Success
    Cor


    Tuesday, May 30, 2017 3:19 PM
  • Hi Reed,

    I saw you wrote in the thread "Food for Thought" that there are MSFT moderators who think they always must interfere in questions. 

    I did not understand that you did mean "Only I may interfere wild all kind of wild guesses"




    Success
    Cor


    I'm not sure how anything I wrote in that other thread relates to this one, and it isn't really proper for us to have this side discussion here.  But to answer your question, the OP wrote:

    "Anything anyone can provide direction wise would be greatly appreciated.."

    Whenever a 3rd party assembly is involved, one of the first troubleshooting steps is commonly to remove that assembly (if possible) and see if the problem persists.  Yes, I am guessing that this WinSCP assembly is the culprit, but it is not a completely baseless guess.  Searching that name, and looking at the most relevant result, I can see that the assembly includes "fancy" GUI features and is in general feature-loaded.  So there is a fairly good chance that somewhere in the assembly there is some desktop-dependent code which is required for proper operation.  Or it may be something user-dependent and the service needs to run as a specific user.  The point is that this is a good place to start looking for the problem.  If it is with the assembly, maybe there is a workaround and maybe there isn't.

    At the end of the day I consider the guess at a possible place to look for a solution better than being told that it isn't possible to receive any help.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 30, 2017 7:47 PM
    Moderator


  • I'm not sure how anything I wrote in that other thread relates to this one, and it isn't really proper for us to have this side discussion here.  But to answer your question, the OP wrote:


    I see you got points again for this message.  

    There is nothing related to VB, probably you got it from a person who has an inferiority complex or something like that. 

    This is what you wrote in that thread.

     

    I don't know what's happened, but we seem to have slipped back a decade to CSGs who are interested in trying to compete with the regular community rather than supplement it.

    My opinion is that with the message in this thread you call the cattle black.  


    Success
    Cor


    Tuesday, May 30, 2017 10:08 PM
  • I don't know what's happened, but we seem to have slipped back a decade to CSGs who are interested in trying to compete with the regular community rather than supplement it.

    My opinion is that with the message in this thread you call the cattle black.

    I fail to see the correlation. I would consider myself a community member first and a moderator second - that's certainly the order in which they occurred. I also don't see where I could be considered "competing" with anyone else in this thread as there were no other posts offering assistance.  But you are entitled to your opinion, so we'll just leave it at that.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 31, 2017 1:07 AM
    Moderator
  • Cor,

    Imagine how all of this looks to Dan.

    You do remember Dan, right? The OP??


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 31, 2017 1:19 PM