Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have written a Windows Service in VB.Net. It is getting installed properly on machine.
But when i start the service, a message comes as The <servicename> on local computer started and then stopped.some services stop automatically it they are not in use by other services or programs".

After this i wrote same service in C#.Net and it is working very well.

VB.Net code
Public Class EBService : Inherits ServiceBase
    Private tia As System.Timers.Timer
    Dim objExport As clsExport
    Public Sub EBService()
        InitializeComponent()
        tia = New System.Timers.Timer()
        GC.KeepAlive(tia)
        tia.Interval = 100000
        tia.Enabled = True

        AddHandler tia.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf OnTimedEvent)
    End Sub


    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Try
tia.Stop()

            clsGlobal.WriteText("START EB SERVICE" & " " & System.DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"))

            clsGlobal.WriteText("END EB SERVICE" & " " & System.DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"))

            tia.Start()
        Catch ex As System.Exception
            ' clsGlobal.WriteText(ex.Message)
        End Try
    End Sub


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        tia.Start()
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        tia.Stop()
    End Sub

End Class


C#.Net Code
namespace MSPOS.EB.TestService
{
    public partial class MSPOSTestService : ServiceBase
    {
        private System.Timers.Timer Tia;


        public MSPOSTestService()
        {
            InitializeComponent();

            Tia = new System.Timers.Timer();
            GC.KeepAlive(Tia);
            Tia.Interval = 60000;
            Tia.Enabled = true;
            Tia.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
        }
        private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Tia.Stop();
                clsGlobal.WriteText("START EB SERVICE" + " " + System.DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"));

                  clsGlobal.WriteText("END EB SERVICE" + " " + System.DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt"));

                Tia.Start();
            }
            catch (System.Exception ex)
            {
                clsGlobal.WriteText(ex.Message);
            }
        }

        protected override void OnStart(string[] args)
        {
tia.Start();
        }

        protected override void OnStop()
        {
tia.Stop();
        }
    }


Can anyone plz tell me what should i do in VB.Net to properly start and stop my service.

Thanks,
Nagendra.
Posted
Updated 12-Jul-11 0:20am
v3

1 solution

The VB version doesn't have tia.Stop() at the top of the tick event handler.

I have to ask why you're stopping/starting the timer like that though. Instead of doing that, set a flag indicating the timer event is busy, and if it is when a timer event comes through, just exit the method:

C#
public partial class MyService
{
    bool isBusy = false;

    private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        if (!isBusy)
        {
            isBusy = true;
            // ... do some processing
            isBusy = false;
        }
    }

}
 
Share this answer
 
Comments
nagendrathecoder 12-Jul-11 6:21am    
Sorry that was by mistake missed, i corrected it.
What are you saying is right but i don't think timer start and stop is causing any problem because it is working fine in C#, or is it so?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900