启动Windows服务时出现问题

启动Windows服务时出现问题

本文介绍了启动Windows服务时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我已经在VB.Net中编写了Windows服务.它已正确安装在机器上.
但是,当我启动该服务时,会出现一条消息,其中< servicename>在本地计算机上启动然后停止.在某些服务没有被其他服务或程序使用的情况下,它们会自动停止."

之后,我在C#.Net中编写了相同的服务,并且运行良好.

VB.Net代码

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代码



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();
        }
    }



谁能告诉我在VB.Net中我应该怎么做才能正确启动和停止我的服务.

谢谢,
Nagendra.



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

Thanks,
Nagendra.

推荐答案

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;
        }
    }

}


这篇关于启动Windows服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:08