IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C#][service windows] problème de débutant avec 1 timer


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2004
    Messages : 93
    Points : 61
    Points
    61
    Par défaut [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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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();
    		}		
    	}
    }

  2. #2
    Expert éminent
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Points : 7 879
    Points
    7 879
    Par défaut
    salut
    Ton souci est peut être ceci
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    je te suggères d'utiliser l'un des deux autres Timers
    * System.Timers.Timer
    * System.Threads.Timer

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    93
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mars 2004
    Messages : 93
    Points : 61
    Points
    61
    Par défaut
    trop fort, c'était bien à cause du type de timer. M E R C I !

    voici le code modifié (et qui fonctionne cette fois)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.Timers.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.Timers.Timer();
    			((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.Elapsed += new System.Timers.ElapsedEventHandler(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.Timers.ElapsedEventArgs e)
    		{
    			Execute();
    		}		
    	}
    }
    developpez.com, c'est vraiment excellent.

  4. #4
    Expert éminent
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Points : 7 879
    Points
    7 879
    Par défaut
    Merci à toi d'avoir mis ton code
    c'est l'esprit des forums ici, il se peut je te dises un jour une connerie mais il y auras toujours quelqu'un pour réajuster, c'est cette entraide qui est à la fois efficace et profitable
    bonne prog
    n'oublies pas 'resolu' bouton en bas à gauche

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Petit problème de débutant avec les tableaux
    Par crazy_zakaria dans le forum Débuter
    Réponses: 10
    Dernier message: 20/11/2009, 10h25
  2. Problème [gros débutant!] avec malloc
    Par Nival dans le forum Débuter
    Réponses: 5
    Dernier message: 13/03/2009, 17h05
  3. Service Windows : problème avec état du service
    Par dlayla4 dans le forum Windows Forms
    Réponses: 5
    Dernier message: 23/05/2008, 09h35
  4. problème de débutant avec la librairie glut sous dev C++
    Par mozillo3625 dans le forum Windows
    Réponses: 0
    Dernier message: 30/11/2007, 22h56
  5. Réponses: 9
    Dernier message: 25/04/2007, 15h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo