启动Windows服务时出现问题 [英] Problem while starting windows service

查看:87
本文介绍了启动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.

推荐答案

VB版本的滴答事件处理程序顶部没有tia.Stop().

我不得不问为什么你要像这样停止/启动计时器.代替执行此操作,设置一个标志,指示计时器事件正忙,如果计时器事件通过,则退出该方法:

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:

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服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆