Temporizador C#
(1/1)
Meta:
Hola:
En el formulario tengo un label y dos buttons. En el label muestra 00:00:00, que son horas, minutos, segundos.
El primer botón empieza llamado Start el contador, el otro botón llamado Stop es detener el temporizador y se pone a 00:00:00 y se queda detenido.
¿Cómo se hace en C#?
Por ahora he hecho esto.
Código
public class form1{
private Timer _timer = null;
public void Form_Load(...){
_timer= new Timer();
_timer.Interval = 1000; // 1seg
_timer.tick += timer_Tick;
}
private void btnIniciar_Click(...){
_timer.Start();
}
private void btnIniciar_Click(...){
_timer.Stop();
}
private void timer_Tick(...){
label.Text = DateTime.Now.ToString("HH:mm:ss")
}
}
La salida muestra la hora, quiero que muestre 00:00:00 cuando está en Stop, que empieze a contar el tiempo, cuando le de desconectar, se pone a 00:00:00. El problema que me muestra la hora actual, y no deseo eso.
Saludos.
Eleкtro:
Un Timer no es un "cronómetro" con el que medir intervalos de tiempo, y, aunque si que se puede utilizar con dicha finalidad, resultaría muy ineficiente en la medida.
Solución:
Utiliza la class StopWatch para medir, y el Timer para notificar del transcurso actual del StopWatch.
https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
Saludos
Meta:
Hola:
Parece más complicado de lo que creía. Tengo que usar 16 temporizadores que al activar uno de ellos empieza por 00:00:00 (HH:MM:SS), y le doy el otro botón parar, se detiene y se pone a 00:00:00 de nuevo.
Por ahora pruebo con dos botones y un contador, lo llamo contador o temporizador o cronómetro para llamarlo de alguna manera.
DEja ver si me sale, luego aviso.
Eleкtro:
Cita de: Meta en 23 Diciembre 2015, 06:26 am
Parece más complicado de lo que creía.
Es algo facil, mira un ejemplo:
Vb.Net:
Código
Public NotInheritable Class Form1 : Inherits Form
Private stopwatchs As Stopwatch() = Enumerable.Repeat(New Stopwatch, 15).ToArray
Friend Timer1 As New Windows.Forms.Timer With {.Interval = 1000, .Enabled = False}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Me.Timer1.Tick, AddressOf Timer1_Tick
End Sub
Private Sub Button1_Click() Handles Button1.Click
' Iniciar stopwatchs...
For Each stw As Stopwatch In Me.stopwatchs
stw.Start()
Next stw
Me.Timer1.Enabled = True
Me.Timer1.Start()
End Sub
Private Sub Button2_Click() Handles Button2.Click
' Detener stopwatchs...
For Each stw As Stopwatch In Me.stopwatchs
stw.Stop()
Next stw
Me.Timer1.Stop()
Me.Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs)
For Each stw As Stopwatch In Me.stopwatchs
Console.WriteLine(stw.Elapsed.ToString)
Next stw
End Sub
End Class
C#:
Código
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public sealed class Form1 : Form
{
private Stopwatch[] stopwatchs = Enumerable.Repeat(new Stopwatch(), 15).ToArray;
internal Windows.Forms.Timer Timer1 = new Windows.Forms.Timer {
Interval = 1000,
Enabled = false
};
private void Form1_Load(object sender, EventArgs e)
{
this.Timer1.Tick += Timer1_Tick;
}
private void Button1_Click()
{
// Iniciar stopwatchs...
foreach (Stopwatch stw in this.stopwatchs) {
stw.Start();
}
this.Timer1.Enabled = true;
this.Timer1.Start();
}
private void Button2_Click()
{
// Detener stopwatchs...
foreach (Stopwatch stw in this.stopwatchs) {
stw.Stop();
}
this.Timer1.Stop();
this.Timer1.Enabled = false;
}
private void Timer1_Tick(object sender, EventArgs e)
{
foreach (Stopwatch stw in this.stopwatchs) {
Console.WriteLine(stw.Elapsed.ToString);
}
}
public Form1()
{
Load += Form1_Load;
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
Meta:
Gracias.
Por lo que veo, hay que usar un timer si o si. En mi caso 16 timer, ejejjeje, y que sea indepentiende cada uno.
Sigo investigadno y me fijaré en el fondo tu código.
;)
Navegación