Heure en temps réel

Contenu du snippet

Ce code affiche l'heure dans un "form" en temps réel. Deux thread sont crées :

1) Le "form"

2) Un thread appelé time Thread qui calcule l'heure, dort une seconde calcule l'heure, dort une seconde .... (en boucle) A chaque fois que le thread calcule l'heure, il l'affiche dans la "form".

Source / Exemple :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace MyTime
{

	/// <summary>
	/// Summary for MyTime
	/// </summary>
	public class MyForm : System.Windows.Forms.Form
	{
	

		//{{OBJ_DECLARATION(MyTime.MyForm)
		protected System.Windows.Forms.Label timeLabel;
		//}}OBJ_DECLARATION
		
		private Thread timeThread;
	
		public MyForm()
		{
		// Required for Visual Forms support
			InitializeComponent();

		// TODO: Add any constructor code after InitializeComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		/// <summary>
		/// Required method for Visual Forms support - do not modify
		/// the contents between the //{{ ... //}} tags.
		/// </summary>
		private void InitializeComponent()
		{			
			//{{OBJ_INSTANTIATION(MyTime.MyForm)
			this.timeLabel = new System.Windows.Forms.Label();
			//}}OBJ_INSTANTIATION
			
			this.SuspendLayout();
						
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			
			//{{OBJ_PROPERTIES(MyTime.MyForm.timeLabel)
			this.timeLabel.Location = new System.Drawing.Point(30, 30);
			this.timeLabel.Size = new System.Drawing.Size(140, 40);
			this.timeLabel.TabIndex = 0;
			this.timeLabel.Font = new System.Drawing.Font("Modern", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(255)));
			//}}OBJ_PROPERTIES
			
			this.timeLabel.Text = this.returnTime();

			//{{FORM_PROPERTIES(MyTime.MyForm)
			this.ClientSize = new System.Drawing.Size(200, 100);
			this.Font = new System.Drawing.Font("MS Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Controls.Add(this.timeLabel);
			//}}FORM_PROPERTIES
			
			this.Text = this.returnDate();
			
			this.Name = "MyForm";
			//this.Text = "MyForm"; 
			
			this.ResumeLayout(false); 
			
			
			timeThread = new Thread(new ThreadStart(timeLive));
			timeThread.Start();		
		}
		
		
		string returnDate(){
			string dateNow = null;
			DateTime now = DateTime.Now;
			dateNow = now.Day + "." + now.Month + "." + now.Year ;
			return dateNow;
		}
		
		
		string returnTime(){
			string timeNow = null;
			int hour;
			int minute;
			int second;
			string hS, mS, sS;

			DateTime now = DateTime.Now;
			hour = now.Hour;
			minute = now.Minute;
			second = now.Second;

			if( hour < 10 )
				hS = "0" + hour;
			else
				hS = "" + hour;
				
			if( minute < 10)
				mS = "0" + minute;
			else
				mS = "" + minute;
				
			if( second < 10 )
				sS = "0" + second;
			else 
				sS = "" + second;
			
			timeNow = hS + ":" + mS + ":" + sS ;			
			return timeNow;
		}
		
		private void timeLive()
		{
			while(true)
			{
				this.timeLabel.Text = this.returnTime(); 	
				Thread.Sleep(1000);
			}
		}
		

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main() 
		{

			Application.Run(new MyForm());
		}

	}
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.