Timer.Start () funciona solo en la primera llamada?

Estoy usando System.Windows.Forms.Timer to display a transfer file update progress(timeleft, speed ..etc)

and I also use backgroundworker to send the file

  • backgroundWorker1_DoWork llamadas timer1.Start();
  • backgroundWorker1_RunWorkerCompleted llamadas timer1.Stop();

It works fine only in the first call for timer1.Strat, but when it called again after timer1.Stop(). No funciona.

timer1.Enabled = True; 
timer1.Interval = 1000;
private void timer1_Tick(object sender, EventArgs e)
    {
        long speed = sumAll - prevSum;
        prevSum = sumAll;
        labelSpeed.Text = CnvrtUnit(speed) + "/S";
        if (speed > 0)
        {
            totalSeconds++;
            labelTime.Text = FormatRemainingText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed));
            labelTotalTime.Text = FormatRemainingText(TimeSpan.FromSeconds(totalSeconds));
        }
    }

¿Qué le pasa y cómo lo soluciono?

preguntado el 10 de marzo de 12 a las 04:03

What is sumAll defined as? And how do you know its not been called. Do you breakpoint in Debug mode and it never hits? -

sumAll is the sentBytes .. like [512k]/23340K No, i didn't say that it's not been called ... it called but not working .. it gave me no exception .. just the labels don't update anymore. -

I thought so. Seriously though. The debugger is your friend. Most of the questions you post here on SO could be solved with some simple debugging skills. Set a breakpoint on labelTime.Text = FormatRemainingText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed)); and see if it ever gets called after the first iteration. If not then your calculations are wrong... fix them. -

Its hard to know what you are doing wrong without showing the actual method you stop and start on. Why don't you just set auto restart to true? I would also put a break point on the start method you call after the first time to see if its being reached. -

ok .. i think System.Timers.Timer would be much better .. it has AUTO REST thing.. -

1 Respuestas

I've figured it out, I use System.Timers.Timer en lugar de System.Windows.Forms.Timer

System.Timers.Timer timer1 = new System.Timers.Timer(1000);

In class constructor I added:

    public FileTransfer()
    {
        InitializeComponent();
        timer1.Elapsed += timer1_Tick;
    }


    private void timer1_Tick(object sender, EventArgs e)
    {
        long speed = sumAll - prevSum;
        Console.WriteLine(speed);
        prevSum = sumAll;
        Speed(CnvrtUnit(speed) + "/S");
        if (speed > 0)
        {
            totalSeconds++;
            Timeleft(FormatRemainingText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed)));
            TotalTime(FormatRemainingText(TimeSpan.FromSeconds(totalSeconds)));
        }
    }

    private void Timeleft(string value)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<string>(Timeleft), new object[] { value });
            return;
        }
        labelTime.Text = value;
    }
    private void TotalTime(string value)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<string>(TotalTime), new object[] { value });
            return;
        }
        labelTotalTime.Text = value;
    }
    private void Speed(string value)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<string>(Speed), new object[] { value });
            return;
        }
        labelSpeed.Text = value;
    }

Now it works every time I call timer1.Start(), no need to "AutoReset".

Respondido el 28 de enero de 16 a las 01:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.