Windows服务启动,运行计时器作业一次然后退出? [英] Windows Service Starts, Runs Timer Job Once then Quits?

查看:82
本文介绍了Windows服务启动,运行计时器作业一次然后退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。



I have the following code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Globalization;
using System.Timers;
using System.IO;
using WindowsService1.ServiceReference1;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public static double infinity = double.PositiveInfinity;
        public static int pingCount = 0;
        public static long avgRtt;
        public static System.Timers.Timer timer1;
        public static System.Timers.Timer timer2;
        public static Service1Client client = new Service1Client();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            timer1 = new System.Timers.Timer();
            timer1.Interval = Convert.ToDouble(3000); //timer intraval in milliseconds
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
            timer1.Enabled = true;

            timer2 = new System.Timers.Timer();
            timer2.Interval = Convert.ToDouble(3000); //timer intraval in milliseconds
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Tick);





        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Latency lat = new Latency();
            //       lat.stationName = "me";
            //       lat.stationip = "myip";
            //       lat.latency = 2;

            //       client.submitData(lat);
            calcLat();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            SqlConnection cs = new SqlConnection(@"Server=130-ASQL1-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            SqlCommand cmd = new SqlCommand("select * from NIM_Latency where stationName='" + System.Environment.MachineName + "'", cs);

            cmd.CommandType = CommandType.Text;

            cmd.Connection = cs;

            cs.Open();

            SqlDataReader dr = cmd.ExecuteReader();


            dr.Read();
            if (dr.HasRows)
            {


                IPHostEntry host;
                string localIP = "";
                host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        localIP = ip.ToString();
                    }
                }

                Latency lat = new Latency();
                lat.stationName = System.Environment.MachineName;
                lat.latency = avgRtt;
                lat.stationip = localIP;


                
                client.updateData(lat);

                dr.Close();
                pingCount = 0;
                client.Close();
                cs.Close();


            }
            else
            {

                IPHostEntry host;
                string localIP = "";
                host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        localIP = ip.ToString();
                    }
                }

                Latency lat = new Latency();
                lat.stationName = System.Environment.MachineName;
                lat.latency = avgRtt;
                lat.stationip = localIP;

                client.submitData(lat);

                pingCount = 0;

                dr.Close();
                cs.Close();
                client.Close();
            }
        }



        protected override void OnStop()
        {
        }

        public void calcLat()
        {
            //client.Open();

            var ping = new Ping();

            var reply = ping.Send("10.209.224.100", 3000);

            avgRtt = (avgRtt * pingCount++ + reply.RoundtripTime) / pingCount;

            pingCount++;

            if (pingCount == 4)
            {
                timer1.Stop();
                timer2.Enabled = true;
            }
        }
    }
}





当我启动服务时,计时器作业运行一次,然后服务自行停止。我一直在做研究,我看到很多关于垃圾收集器可能会停止服务的讨论。这是我的第一个Windows服务,所以任何帮助都会很棒。



When I start the service the timer job runs once and then the service stops on it's own. I've been doing research and i'm seeing alot of talk about garbage collector possibly stopping the service. This is my first windows service so any help would be great.

推荐答案

这是因为你没有任何支持Windows服务工作的代码。通常, OnStart 方法创建一个或多个执行所有工作的线程。计时器可能需要也可能不需要,但几乎总是更好,更容易避免使用任何计时器。



-SA
This is because you don't really have any code supporting Windows Service work. Typically, OnStart method creates one or more threads which do all the work. The timer may or may not be needed, but it's almost always better and easier to avoid using any.

—SA


这篇关于Windows服务启动,运行计时器作业一次然后退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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