[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Windows Services

Posted on 2006-10-24
6
Medium Priority
?
238 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 total points
ID: 17808253
Put this in the OnStart:

       AddHandler Timer1.Elapsed, AddressOf Timer1_Tick
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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