Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 23 Diciembre 2015, 00:16 AM

Título: Temporizador C#
Publicado por: Meta en 23 Diciembre 2015, 00:16 AM
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 (csharp) [Seleccionar]
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.
Título: Re: Temporizador C#
Publicado por: Eleкtro en 23 Diciembre 2015, 00:46 AM
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.

Saludos
Título: Re: Temporizador C#
Publicado por: Meta en 23 Diciembre 2015, 06:26 AM
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.
Título: Re: Temporizador C#
Publicado por: Eleкtro en 23 Diciembre 2015, 06:42 AM
Cita de: Meta en 23 Diciembre 2015, 06:26 AMParece más complicado de lo que creía.

Es algo facil, mira un ejemplo:

Vb.Net:
Código (vbnet) [Seleccionar]
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 (csharp) [Seleccionar]

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)
//=======================================================
Título: Re: Temporizador C#
Publicado por: Meta en 23 Diciembre 2015, 09:55 AM
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.

;)