Solved

Windows Services

Posted on 2006-10-24
6
215 Views
Last Modified: 2010-04-23
Ive been trying to write a windows service from the tutorial at "http://www.devarticles.com/c/a/VB.Net/Creating-A-Windows-Service-in-VB.NET/1/", I can write the code, and build correctly, even install the service, however I cannot seem to get it to do anything!

The code from the tutorial I have simplified just to see if I can see it running (see if its a problem connecting to the event viewer) so the code I currently have is:-

Public Class Service1
    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer1.Enabled = True
    End Sub
    Protected Overrides Sub OnStop()
        Timer1.Enabled = False
    End Sub
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        MsgBox("Im running")
    End Sub
End Class

But still doesnt work, does anyone have any suggestions?

I can see the service has been started in my services view!

Tony Cross
0
Comment
Question by:tonelm54
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17794388
>  MsgBox("Im running")

you will never see any interface from a service, hence the msgbox will actually "block" your service.
if you want a front-end (for example in systray), you need to develop a second application that sits in the tray and communicates with the service.

0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17794412
Open services, find your service and check the  : Allow service to interact with desktop, under the "Log On" tab.
And see if that makes any difference.
0
 

Author Comment

by:tonelm54
ID: 17800380
Ok, tried the 'interact with desktop' no luck, although I have changed my code to take that problem out.

Public Class Service1
    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer1.Interval = 1
        Timer1.Enabled = True
        writefile("Service Started")
    End Sub
    Protected Overrides Sub OnStop()
        Timer1.Enabled = False
        writefile("Service Stopped")
    End Sub
    Private Sub writefile(ByVal strMessage As String)
        FileOpen(1, "c:\test.txt", OpenMode.Append)
        PrintLine(1, strMessage & " " & Now.Hour & ":" & Now.Minute & "." & Now.Millisecond)
        FileClose(1)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        writefile("Tick1 Running")
    End Sub
End Class


It creates the line 'Service Started' and 'service stopped' lines, but does not display the 'Tick1 Running', I run as a console (changing the project type in properties) and set it to console, and it works great. Any ideas?

Tony Cross
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 21

Expert Comment

by:mastoo
ID: 17800615
Be sure you're using either the System.Timers.Timer or System.Threading.Timer.  The System.Windows.Forms.Timer shouldn't be used in a service.
0
 

Author Comment

by:tonelm54
ID: 17807646
Ok, tried this now, still get the start and end service messages, but still dont get my ticks, any ideas?

Public Class Service1
    Dim Timer1 As New System.Timers.Timer
    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer1.Interval = 1
        Timer1.Enabled = True
        writefile("Service Started")
    End Sub
    Protected Overrides Sub OnStop()
        Timer1.Enabled = False
        writefile("Service Stopped")
    End Sub
    Private Sub writefile(ByVal strMessage As String)
        FileOpen(1, "c:\test.txt", OpenMode.Append)
        PrintLine(1, strMessage & " " & Now.Hour & ":" & Now.Minute & "." & Now.Millisecond)
        FileClose(1)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        writefile("Tick1 Running")
    End Sub
End Class
0
 
LVL 21

Accepted Solution

by:
mastoo earned 500 total points
ID: 17808253
Put this in the OnStart:

       AddHandler Timer1.Elapsed, AddressOf Timer1_Tick
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question