Problems with threads / timers when creating a system service under VB.NET 4

I thought building a simple system service under VB.NET would be part of the cake. Boy, I was wrong. First, I find general posts via Google that say you should use Windows.Forms.Timer. Then I found conflicting information which says that you have to deduce the timer from the timer class with code for it to work. People then report any issues using the different types of streams available. I cannot debug threads with the Just-in-Time VS 2010 debugger for obvious reasons (although, I don't know if there is an alternative method for this). Wow, how confusing it all is when there is no definitive guide ...

My project is a launcher application (similar to cron) that will fire periodically for a certain number of seconds. I am trying to use Process.Start () method. I have a Beep () function as the first command and Process.Start along with a Process.WaitForExit method so that it blocks as the last instruction. I had code doing this through timers, but now I am starting to use threads. There is no performance difference. The overloaded OnStart method gets triggered (thanks to debugging I'm sure), but nothing happens when the service starts in production, as if it were ignoring all of my code. Putting loops and logic in the OnStart method gives a process that won't start. I know this is a threading issue, but I also know that it should be required to use threads. I am now dumbfounded how to make this work.I am curious to know the solution.

In addition to the service class, I have ServiceInstaller and ProcessInstaller installed, which I copied verbatim from MSDN.

Here is the code I am trying to work with. Note that this simply reflects the current state of my code when trying to implement logic on a thread instead of a timer (which would be optimal for me):

    Dim config As String
    Dim configValues(2) As String
    Dim objReader As System.IO.StreamReader
    Dim interval As Integer
    Dim launchProcess As Process

    While True
        Beep()
        ' This sub runs every time the elapsed milli-seconds of the timer pass
        ' Sp add your code here.

        config = "30 c:\WiFiDropOutFix\start.vbs"

        Try
            objReader = New System.IO.StreamReader("C:\WiFiDropOutFix\config.txt")
            config = objReader.ReadToEnd
            objReader.Close()
        Catch ex As Exception

        End Try

        configValues = config.Split(New Char() {" "c})
        interval = Convert.ToInt32(configValues(0))

        If Not interval > 0 And Not interval < 61 Then
            interval = 10
        End If

        launchProcess = Process.Start(configValues(1))
        launchProcess.WaitForExit()

        Thread.Sleep(interval * 1000)

    End While
End Sub

      

Here is my OnStart code as requested:

Protected Overrides Sub OnStart(ByVal args() As String)

    appLaunchLogicThread = New Thread(AddressOf appLaunchLogic)
    appLaunchLogicThread.Start()

End Sub

      

TIA.

+3


source to share


2 answers


Try going to the Tools Control Panel, Editing Your Service Properties, and checking the Online Service or Allow the service to interact with the desktop option . If you try to start a new process and see it on the screen, nothing will happen if this option is not enabled.

Also, set the Administrator service logon credentials to make sure this is not a permissions issue . If it works, you have found a problem.



One more tip: write all processing code as a normal applicationwhich you can debug easily, and once all errors are removed and you know the code is flawless, then implement it as a service .

+1


source


Ok, I managed to find the reason why my project was not working. I do remind you that someone said that the [STAThread] directive is needed to start the processes running inside the service and should be placed just before the main function. Codeproject has released a cron project for Windows Services in C # and uses this directive. I'll have to look more at this released project, but maybe it can't be implemented in VB.NET. At this point, I don't understand how to enable the service launcher through the main function / class using the Visual Studio VB.NET service template, as it seems to work by itself.



Also, I finally managed to find a Windows cron service that will run in a few seconds, which does not have a mandatory commercial license, can be used for free, and has an available registration (from what is stated on the website). It's called WinCron .

0


source







All Articles