WindowsService不执行

时间:2017-03-31 18:46:33

标签: c# while-loop windows-services

我正在做一个小的Windows服务,它从Web服务读取并将数据存储在本地数据库中,但是,当我在调试它时,它没有完成执行while(虽然它是一个服务) vs 15它运作完美,请遵循以下代码。

PS:我用来调试的代码我在它开始之前对它进行注释,它到达while时间,但只有第一行并且不检查其余部分。

Program.cs的

using System.ServiceProcess;

namespace rdi_server.service
{
static class Program
{
    static void Main()
    {

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}
}

Service1.CS

using System;
using System.Configuration;
using System.ServiceProcess;
using System.Threading;

namespace rdi_server.service
{
    public partial class Service1 : ServiceBase
{

    private Thread _thread;

    private Updater _updater = new Updater();

    private readonly int Interval = Convert.ToInt32(ConfigurationManager.AppSettings["timer"]);
    private readonly string Connection = ConfigurationManager.AppSettings["connection"];

    private readonly Connector _usuarioConnector;
    private readonly Connector _bandaConnector;
    private readonly Connector _generoConnector;
    private readonly Connector _musicaConnector;

    public Service1()
    {
        _usuarioConnector = new Connector("UsuarioBase");
        _bandaConnector = new Connector("BandaBase");
        _generoConnector = new Connector("GeneroBase");
        _musicaConnector = new Connector("MusicaBase");

        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        StartDebug();
    }

    public void StartDebug()
    {
        _thread = new Thread(OnTime);
        _thread.Name = "My Worker Thread";
        _thread.Start();
    }

    protected override void OnStop()
    {

    }

    protected void OnTime()
    {
        while (true)
        {
            EventLog.WriteEntry("Dentro do WHILE foi executado!");
            _updater.Do(Connection, _usuarioConnector, _generoConnector, _bandaConnector, _musicaConnector);
            EventLog.WriteEntry("Fim do while");
            Thread.Sleep(Interval);
        }
    }
}
}

Updater.CS

using rdi_musica.core;
using System.Collections.Generic;
using System.Data.SqlClient;

    namespace rdi_server.service
    {
    class Updater
    {
    public void Do(string Connection,
                        Connector usuario,
                        Connector genero,
                        Connector banda,
                        Connector musica)
    {

        var usuarios = Loader.LoadUsuarios(usuario);
        var generos = Loader.LoadGeneros(genero);
        var bandas = Loader.LoadBandas(banda);
        var musicas = Loader.LoadMusicas(musica);


        using (var connection = new SqlConnection(Connection))
        {
            connection.Open();

            foreach (var _usuario in usuarios)
            {
                DomainDAO.InsertUsuario(connection, _usuario);
            }
            foreach (var _genero in generos)
            {
                DomainDAO.InsertGenero(connection, _genero);
            }
            foreach (var _banda in bandas)
            {
                DomainDAO.InsertBanda(connection, _banda);
            }
            foreach (var _musica in musicas)
            {
                DomainDAO.InsertMusica(connection, _musica);
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

可能发生的事情是你在某个地方发生了未处理的异常,这会导致你的while循环失效。

您可以将Windows事件日志与try-catch结合使用来记录错误,而不会破坏无限循环:

protected void OnTime()
{
    while (true)
    {
        try
        {
            EventLog.WriteEntry("Dentro do WHILE foi executado!");
            _updater.Do(Connection, _usuarioConnector, _generoConnector, _bandaConnector, _musicaConnector);
            EventLog.WriteEntry("Fim do while");
        }
        catch (Exception ex)
        {
            this.EventLog.WriteEntry("ERROR: " + ex.GetType().ToString() + " : " + ex.Message + " : " + ex.StackTrace, EventLogEntryType.Error);
        }

        Thread.Sleep(Interval);
    }
}