RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 965332
Accepted
Olejan
Olejan
Asked:2020-04-04 15:18:48 +0000 UTC2020-04-04 15:18:48 +0000 UTC 2020-04-04 15:18:48 +0000 UTC

函数退出时应该停止计时器,还是垃圾收集器会处理它?

  • 772

我使用Stopwatch计时器来计时我的项目功能中代码段的执行。以一种好的方式,在退出我使用计时器的功能之前,我必须停止它。但有时一个函数中有很多退出点,在每次return退出前停止定时器并不是很方便。问题是 - 它是否充满了一些复杂性 - 退出功能而不停止计时器,或者它不重要,因为。当函数退出时,定时器的本地实例是否会被垃圾收集器销毁(或者可能不会)?

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    A K
    2020-04-04T15:33:44Z2020-04-04T15:33:44Z

    这是一个普通的类,它在变量范围之外的命运是很普通的。

    不会出现“突然,不停止计时器,另一个时钟会出错”、“与数据库的连接将断开”或“会发生原子爆炸”之类的并发症——你可以查看反编译的源代码并查看代码,这个类甚至没有实现IDisposable,所以理解上没有任何困难。

    // Decompiled with JetBrains decompiler
    // Type: System.Diagnostics.Stopwatch
    // Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // MVID: 988FA076-10C9-4365-AE6D-295A6AA379FE
    // Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll
    
    using Microsoft.Win32;
    
    namespace System.Diagnostics
    {
      /// <summary>Provides a set of methods and properties that you can use to accurately measure elapsed time.To browse the .NET Framework source code for this type, see the Reference Source.</summary>
      [__DynamicallyInvokable]
      public class Stopwatch
      {
        private const long TicksPerMillisecond = 10000;
        private const long TicksPerSecond = 10000000;
        private long elapsed;
        private long startTimeStamp;
        private bool isRunning;
        /// <summary>Gets the frequency of the timer as the number of ticks per second. This field is read-only.</summary>
        [__DynamicallyInvokable]
        public static readonly long Frequency;
        /// <summary>Indicates whether the timer is based on a high-resolution performance counter. This field is read-only.</summary>
        [__DynamicallyInvokable]
        public static readonly bool IsHighResolution;
        private static readonly double tickFrequency;
    
        static Stopwatch()
        {
          if (!SafeNativeMethods.QueryPerformanceFrequency(out Stopwatch.Frequency))
          {
            Stopwatch.IsHighResolution = false;
            Stopwatch.Frequency = 10000000L;
            Stopwatch.tickFrequency = 1.0;
          }
          else
          {
            Stopwatch.IsHighResolution = true;
            Stopwatch.tickFrequency = 10000000.0;
            Stopwatch.tickFrequency /= (double) Stopwatch.Frequency;
          }
        }
    
        /// <summary>Initializes a new instance of the <see cref="T:System.Diagnostics.Stopwatch" /> class.</summary>
        [__DynamicallyInvokable]
        public Stopwatch()
        {
          this.Reset();
        }
    
        /// <summary>Starts, or resumes, measuring elapsed time for an interval.</summary>
        [__DynamicallyInvokable]
        public void Start()
        {
          if (this.isRunning)
            return;
          this.startTimeStamp = Stopwatch.GetTimestamp();
          this.isRunning = true;
        }
    
        /// <summary>Initializes a new <see cref="T:System.Diagnostics.Stopwatch" /> instance, sets the elapsed time property to zero, and starts measuring elapsed time.</summary>
        /// <returns>A <see cref="T:System.Diagnostics.Stopwatch" /> that has just begun measuring elapsed time.</returns>
        [__DynamicallyInvokable]
        public static Stopwatch StartNew()
        {
          Stopwatch stopwatch = new Stopwatch();
          stopwatch.Start();
          return stopwatch;
        }
    
        /// <summary>Stops measuring elapsed time for an interval.</summary>
        [__DynamicallyInvokable]
        public void Stop()
        {
          if (!this.isRunning)
            return;
          this.elapsed += Stopwatch.GetTimestamp() - this.startTimeStamp;
          this.isRunning = false;
          if (this.elapsed >= 0L)
            return;
          this.elapsed = 0L;
        }
    
        /// <summary>Stops time interval measurement and resets the elapsed time to zero.</summary>
        [__DynamicallyInvokable]
        public void Reset()
        {
          this.elapsed = 0L;
          this.isRunning = false;
          this.startTimeStamp = 0L;
        }
    
        /// <summary>Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.</summary>
        [__DynamicallyInvokable]
        public void Restart()
        {
          this.elapsed = 0L;
          this.startTimeStamp = Stopwatch.GetTimestamp();
          this.isRunning = true;
        }
    
        /// <summary>Gets a value indicating whether the <see cref="T:System.Diagnostics.Stopwatch" /> timer is running.</summary>
        /// <returns>
        /// <see langword="true" /> if the <see cref="T:System.Diagnostics.Stopwatch" /> instance is currently running and measuring elapsed time for an interval; otherwise, <see langword="false" />.</returns>
        [__DynamicallyInvokable]
        public bool IsRunning
        {
          [__DynamicallyInvokable] get
          {
            return this.isRunning;
          }
        }
    
        /// <summary>Gets the total elapsed time measured by the current instance.</summary>
        /// <returns>A read-only <see cref="T:System.TimeSpan" /> representing the total elapsed time measured by the current instance.</returns>
        [__DynamicallyInvokable]
        public TimeSpan Elapsed
        {
          [__DynamicallyInvokable] get
          {
            return new TimeSpan(this.GetElapsedDateTimeTicks());
          }
        }
    
        /// <summary>Gets the total elapsed time measured by the current instance, in milliseconds.</summary>
        /// <returns>A read-only long integer representing the total number of milliseconds measured by the current instance.</returns>
        [__DynamicallyInvokable]
        public long ElapsedMilliseconds
        {
          [__DynamicallyInvokable] get
          {
            return this.GetElapsedDateTimeTicks() / 10000L;
          }
        }
    
        /// <summary>Gets the total elapsed time measured by the current instance, in timer ticks.</summary>
        /// <returns>A read-only long integer representing the total number of timer ticks measured by the current instance.</returns>
        [__DynamicallyInvokable]
        public long ElapsedTicks
        {
          [__DynamicallyInvokable] get
          {
            return this.GetRawElapsedTicks();
          }
        }
    
        /// <summary>Gets the current number of ticks in the timer mechanism.</summary>
        /// <returns>A long integer representing the tick counter value of the underlying timer mechanism.</returns>
        [__DynamicallyInvokable]
        public static long GetTimestamp()
        {
          if (!Stopwatch.IsHighResolution)
            return DateTime.UtcNow.Ticks;
          long num = 0;
          SafeNativeMethods.QueryPerformanceCounter(out num);
          return num;
        }
    
        private long GetRawElapsedTicks()
        {
          long elapsed = this.elapsed;
          if (this.isRunning)
          {
            long num = Stopwatch.GetTimestamp() - this.startTimeStamp;
            elapsed += num;
          }
          return elapsed;
        }
    
        private long GetElapsedDateTimeTicks()
        {
          long rawElapsedTicks = this.GetRawElapsedTicks();
          if (Stopwatch.IsHighResolution)
            return (long) ((double) rawElapsedTicks * Stopwatch.tickFrequency);
          return rawElapsedTicks;
        }
      }
    }
    

    PS “秒表类出现在 .NET 2.0 中,从那时起就没有改变。”

    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5