Windows Service Problem?


Jun 4, 2004
Programming Experience
Currently i am building a windows service.
1. How to make a windows service that its icon display on the task bar?
2. How to show a form when we click the icon?
3. I have problem when starting the windows service, if the service have a windows component such as notifyicon, timers etc. If i am using timers from windows component instead of system.timers.timers, the error "System.Reflection.TargetInvocationException" will come out when we start the service. Could the windows service use windows component?
4. does the "timers" is elapsed every time according to the interval or just elapse once for the first time when the timers is enabled?
1. A windows service shouldn't (generally) interact with the desktop. Usually services that show forms or taskBar icons do so by interacting with a seperate executable (that contain the forms and/or taskBar icons).
So to answer your question, create a service and a seperate executable and have interaction between the two.

2. To show a form by clicking the NotifyIcon, simply use the click event handler of the NotifyIcon component; use code similar to:
Private Sub NotifyIcon1_MouseDown(ByVal sender As System.Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) _
  Handles NotifyIcon1.MouseDown
    If e.Button = MouseButtons.Left Then
        Dim frm as new FormName()
    End If
End Sub

3. See #1

4. Do you mean the timer class from the System.Windows.Forms namespace? The timer from that class doesn't have an Elapsed event, it only has a tick event which is executed every time the interval has elapsed (while Enabled).
The timer from the System.Timers namespace has an event called Elapsed which also is executed every time the interval has elapsed (while Enabled).
Timers in the windows service

I am using the timer from the System.Timers namespace.
But when i start the service, the service will stop directly since it has nothing to do according to the message displayed.
I actually have the timer1_elapsed event in the code. I think it should check every time according to the interval. But how come the message "The service stop since it doesn't perform anything" is displayed if i start the service. After the message displayed, it will stop automatically...
Sorry if the English is mess up.
Why are you using a timer ?

I use Threads and the Service starts as it has to execute the thread/s.
You create a Sub or Function Method with a Do ... Loop inside.
Also have your method Code in a Try - Catch - Finally - End try.
The you declare a NEW Thread(Address Of NameOf Method) as a delegate.
In your OnStart you say Thread.Start.
If you want to stop your Thread you call a Thread.Abort and Thread.Join.
Thread.Abort will throw a System.ThreadAbortException (Therefore in your Method you have to have a catch ex as threadabortexception, within this catch you have a Thread.ResetAbort command).
This will cause the Do....Loop to break as it gets a exception.
The Thread.Join event will hold Thread activitie until a Thread is properly terminated.

You should be able to start your Service now.

Latest posts