如何在C#中自动停止计时器..? [英] How to stop a timer automatically in C# ..?

查看:482
本文介绍了如何在C#中自动停止计时器..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我编写了一个程序,通过c#中的串口发送文本文件,并添加一个计时器来计算时间。

当我开始发送文件时,计时器开启。但是当转移完成时它不会停止任何人都可以告诉我如何在文件传输完成时停止/暂停计时器



Hi all,

I have write a program to send text files over serial port in c# , and add a timer to calculate time.
When i start sending file the timer is on. But it won't stop when the transfer is completed can anyone tell me how to stop/pause the timer when file transferring is completed

private void butSendFileEle_Click(object sender, EventArgs e)
       {
           if (serialPort1.IsOpen == false)
           {
               try
               {
                   this.serialPort1.Open();
               }
               catch
               {
               }
           }
           timer1.Start();
           serialPort1.Write(System.IO.File.ReadAllText(this.txtfilePo.Text));

       }







private void timer1_Tick(object sender, EventArgs e)
       {
           label2.Text = min + ":" + sec + ":" + ms + ":" + us.ToString();
           ms++;
           if (us > 10)
           {
               ms++;
               us = 0;
           }
           else
           {
               us++;
           }
           if (ms > 100)
           {
               sec++;
               ms = 0;
           }

       }





这是我的代码



我尝试了什么:



这是正确的方法吗?如果没有,任何人都可以建议我计算持续时间的正确方法



提前致谢



This is my code

What I have tried:

Is this is the correct way ?? if not can anyone suggest me the correct way to calculate time duration

Thanks in advance

推荐答案

秒表比任何计时器都要好得多

这里有一个例子,它可以让你知道何时调用秒表以及如何停止它并打印时间。

Stopwatch is so much better than any timer for this
here is an example which can make understand when to call stopwatch and how and stop it and print the time.
using System.Threading;
 public class Program
    {
        public static void Main(string[] args)
        {
               
            var stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
                Thread.Sleep(100);
            Console.WriteLine("Hello, world!");
            // Your code here.
                int cnt=0;
                for(int i=1; i<=100000;i++)
                {
                    cnt+=1;
                }
            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
            Console.WriteLine("RunTime " + elapsedTime);
            
        }            
            
        }


致电:

Call:
timer1.Stop();

转移完成后 - 即你已经没有数据要发送了。检查 SerialPort.BytesToWrite属性(系统。 IO.Ports) [ ^ ]并且它会给你一个估计 - 并不是所有的字节都会到达desitination,因为端口驱动程序也将拥有它自己的缓冲区,并且端口硬件芯片通常也有一个小的(8字节左右)缓冲区,但你无法直接查询这些缓冲区以获得准确的指示。

When the transfer is complete - I.e. you have run out of data to send. Check the SerialPort.BytesToWrite Property (System.IO.Ports)[^] and it'll give you an estimate - not all the bytes will have arrived at the desitination yet, becasue the port driver will also have it's own buffer, and the port hardware chips generally have a small (8 bytes or so) buffer as well but you can't query those directly to get an accurate indication.


另一种(非常简单)的可能性:

- bevor你把你的数据写入你读到的Serialport现在变成一个变量(类型日期)

- 当你完成Serialport.write后你就建立了差异(就像timeSpan一样)在你的变量和现在之间 - 这也显示你经过的时间(以秒或毫秒 - 你想要的)


这可能是这样的:

Another (very simple) possibility :
- bevor you write your data to the Serialport you read Now into a Variable (type Date)
- when you have finished the Serialport.write you build the difference (as timeSpan) between your Variable and Now - this Shows you also the elapsed time (in Seconds or Milliseconds - like you want)

That could look like this :
System.DateTime myStart = System.DateTime.Now;

serialPort1.Write(System.IO.File.ReadAllText(this.txtfilePo.Text));

System.TimeSpan myTimespan = System.DateTime.Now.Subtract(myStart);
Int myElapsedTime = myTimespan.Seconds * 1000 + myTimespan.Milliseconds;


这篇关于如何在C#中自动停止计时器..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆