Solved

Windows Services

Posted on 2006-10-24
6
209 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now