Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Windows Services

Posted on 2006-10-24
6
222 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 143

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 38
VB.NET 2008 Publish Error 2 32
"Emulate" TAB key when press Enter Key 3 45
MailAddress in vb 4 29
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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