[C#][service windows] problème de débutant avec 1 timer
Bonjour,
j'essaye de faire un service windows qui fait une action (scruter une table d'une DB) à intervalle de temps régulier. Tout laissait à penser que je devais utiliser un Timer, mais voilà : L'action Timer_Tick ne se déclenche jamais. J'essaie en vain depuis pas mal de temps. Voici le code du service si quelqu'un peu me montrer ou est l'erreur dans la programmation du timer. merci ;)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using MAJService.DataLayers;
using System.Configuration;
using System.IO;
using System.Configuration.Install;
namespace MAJService
{
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.IContainer components;
private System.Diagnostics.EventLog eventLog1;
private DataLayer myDataLayer;
private System.Windows.Forms.Timer timer1;
public Service1()
{
InitializeComponent();
string myConn = ConfigurationSettings.AppSettings["SQLConnectionString"];
myDataLayer = new DataLayer(myConn);
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.eventLog1 = new System.Diagnostics.EventLog();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// eventLog1
//
this.eventLog1.Log = "Application";
this.eventLog1.Source = "MAJService";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 6000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Service1
//
this.CanShutdown = true;
this.ServiceName = "MAJService";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
timer1.Interval=6000;
timer1.Start();
eventLog1.WriteEntry("Service démaré");
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
eventLog1.WriteEntry("Service arrété");
}
/// <summary>
/// fonction principale
/// </summary>
/// <param name="?"></param>
private void Execute()
{
eventLog1.WriteEntry("timer tick");
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Execute();
}
}
} |