<legend id='3nkqR'><style id='3nkqR'><dir id='3nkqR'><q id='3nkqR'></q></dir></style></legend>
<tfoot id='3nkqR'></tfoot>

<i id='3nkqR'><tr id='3nkqR'><dt id='3nkqR'><q id='3nkqR'><span id='3nkqR'><b id='3nkqR'><form id='3nkqR'><ins id='3nkqR'></ins><ul id='3nkqR'></ul><sub id='3nkqR'></sub></form><legend id='3nkqR'></legend><bdo id='3nkqR'><pre id='3nkqR'><center id='3nkqR'></center></pre></bdo></b><th id='3nkqR'></th></span></q></dt></tr></i><div id='3nkqR'><tfoot id='3nkqR'></tfoot><dl id='3nkqR'><fieldset id='3nkqR'></fieldset></dl></div>

        <bdo id='3nkqR'></bdo><ul id='3nkqR'></ul>

      1. <small id='3nkqR'></small><noframes id='3nkqR'>

      2. 计时器可以自动垃圾收集吗?

        时间:2023-09-13

        • <i id='CmHny'><tr id='CmHny'><dt id='CmHny'><q id='CmHny'><span id='CmHny'><b id='CmHny'><form id='CmHny'><ins id='CmHny'></ins><ul id='CmHny'></ul><sub id='CmHny'></sub></form><legend id='CmHny'></legend><bdo id='CmHny'><pre id='CmHny'><center id='CmHny'></center></pre></bdo></b><th id='CmHny'></th></span></q></dt></tr></i><div id='CmHny'><tfoot id='CmHny'></tfoot><dl id='CmHny'><fieldset id='CmHny'></fieldset></dl></div>
          <legend id='CmHny'><style id='CmHny'><dir id='CmHny'><q id='CmHny'></q></dir></style></legend>

              <small id='CmHny'></small><noframes id='CmHny'>

                <tbody id='CmHny'></tbody>
            • <tfoot id='CmHny'></tfoot>
                <bdo id='CmHny'></bdo><ul id='CmHny'></ul>

                1. 本文介绍了计时器可以自动垃圾收集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当您使用将在程序的整个生命周期内运行的 TimerThread 时,您是否需要保留对它们的引用以防止它们被垃圾收集了吗?

                  When you use a Timer or a Thread that will just run for the entire lifetime of the program do you need to keep a reference to them to prevent them from being garbage collected?

                  请抛开下面的程序可以将 timer 作为类中的静态变量这一事实,这只是一个展示问题的玩具示例.

                  Please put aside the fact that the below program could have timer as a static variable in the class, this is just a toy example to show the issue.

                  public class Program
                  {
                      static void Main(string[] args)
                      {
                          CreateTimer();
                  
                          Console.ReadLine();
                      }
                  
                      private static void CreateTimer()
                      {
                          var program = new Program();
                  
                          var timer = new Timer();
                          timer.Elapsed += program.TimerElapsed;
                          timer.Interval = 30000;
                          timer.AutoReset = false;
                          timer.Enabled = true;
                      }
                  
                      private void TimerElapsed(object sender, ElapsedEventArgs e)
                      {
                          var timerCast = (Timer)sender;
                  
                          Console.WriteLine("Timer fired at in thread {0}", GetCurrentThreadId());
                  
                          timerCast.Enabled = true;
                      }
                  
                      ~Program()
                      {
                          Console.WriteLine("Program Finalized");
                      }
                  
                      [DllImport("kernel32.dll")]
                      static extern uint GetCurrentThreadId();
                  }
                  

                  可以在上面的示例中收集计时器吗?我运行了一段时间,我从来没有收到异常,也没有消息说 ~Program() 被调用了.

                  Could the timer get collected in that above example? I ran it for a while and I never got a exception nor a message saying ~Program() was called.

                  更新:我从 这个问题中发现(感谢sethcran)线程由CLR跟踪,但我仍然想要关于计时器的答案.

                  UPDATE: I found out from this question (thanks sethcran) that threads are tracked by the CLR, but I still would like an answer about Timers.

                  推荐答案

                  这只是 System.Threading.Timer 类的问题,如果您不在某处存储对它的引用.它有几个构造函数重载,采用 state 对象的重载很重要.CLR 关注该状态对象.只要在某处引用它,CLR 就会将计时器保留在其计时器队列中,并且计时器对象不会被垃圾回收.大多数程序员不会使用那个状态对象,MSDN 文章肯定没有解释它的作用.

                  This is only a problem with the System.Threading.Timer class if you don't otherwise store a reference to it somewhere. It has several constructor overloads, the ones that take the state object are important. The CLR pays attention to that state object. As long as it is referenced somewhere, the CLR keeps the timer in its timer queue and the timer object won't get garbage collected. Most programmers will not use that state object, the MSDN article certainly doesn't explain its role.

                  System.Timers.Timer 是 System.Threading.Timer 类的包装器,使其更易于使用.特别是,只要启用了计时器,它将使用该状态对象并保持对它的引用.

                  System.Timers.Timer is a wrapper for the System.Threading.Timer class, making it easier to use. In particular, it will use that state object and keep a reference to it as long as the timer is enabled.

                  请注意,在您的情况下,计时器的 Enabled 属性在进入您的 Elapsed 事件处理程序时为 false,因为您有 AutoReset = false.因此,一旦计时器进入您的事件处理程序,它就有资格被收集.但是您可以通过引用 sender 参数来避免麻烦,需要将 Enabled 设置回 true.这使得抖动报告参考,所以你没有问题.

                  Note that in your case, the timer's Enabled property is false when it enters your Elapsed event handler because you have AutoReset = false. So the timer is eligible for collection as soon as it enters your event handler. But you stay out of trouble by referencing the sender argument, required to set Enabled back to true. Which makes the jitter report the reference so you don't have a problem.

                  请小心使用 Elapsed 事件处理程序.该方法中抛出的任何异常都将在没有诊断的情况下被吞下.这也意味着您不会将 Enabled 设置回 true.您必须使用 try/catch 来做一些合理的事情.如果你不打算故意结束你的程序,至少你需要让你的主程序知道某些东西不再工作了.将 Enabled = true 放在 finally 子句中可以避免收集计时器垃圾,但有可能让您的程序一遍又一遍地抛出异常.

                  Do be careful with the Elapsed event handler. Any exception thrown inside that method will be swallowed without a diagnostic. Which also means that you won't set Enabled back to true. You must use try/catch to do something reasonable. If you are not going to intentionally end your program, at a minimum you'll need to let your main program know that something isn't working anymore. Putting Enabled = true in a finally clause can avoid getting the timer garbage collected, but at the risk of having your program throw exceptions over and over again.

                  这篇关于计时器可以自动垃圾收集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:静态成员是否曾经被垃圾收集? 下一篇:C# 中的内存泄漏

                  相关文章

                        <bdo id='So8AQ'></bdo><ul id='So8AQ'></ul>
                      <i id='So8AQ'><tr id='So8AQ'><dt id='So8AQ'><q id='So8AQ'><span id='So8AQ'><b id='So8AQ'><form id='So8AQ'><ins id='So8AQ'></ins><ul id='So8AQ'></ul><sub id='So8AQ'></sub></form><legend id='So8AQ'></legend><bdo id='So8AQ'><pre id='So8AQ'><center id='So8AQ'></center></pre></bdo></b><th id='So8AQ'></th></span></q></dt></tr></i><div id='So8AQ'><tfoot id='So8AQ'></tfoot><dl id='So8AQ'><fieldset id='So8AQ'></fieldset></dl></div>
                      <legend id='So8AQ'><style id='So8AQ'><dir id='So8AQ'><q id='So8AQ'></q></dir></style></legend>

                      <small id='So8AQ'></small><noframes id='So8AQ'>

                      <tfoot id='So8AQ'></tfoot>