Solved

Windows Services

Posted on 2006-10-24
6
198 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

24 Experts available now in Live!

Get 1:1 Help Now