<i id='mLmJG'><tr id='mLmJG'><dt id='mLmJG'><q id='mLmJG'><span id='mLmJG'><b id='mLmJG'><form id='mLmJG'><ins id='mLmJG'></ins><ul id='mLmJG'></ul><sub id='mLmJG'></sub></form><legend id='mLmJG'></legend><bdo id='mLmJG'><pre id='mLmJG'><center id='mLmJG'></center></pre></bdo></b><th id='mLmJG'></th></span></q></dt></tr></i><div id='mLmJG'><tfoot id='mLmJG'></tfoot><dl id='mLmJG'><fieldset id='mLmJG'></fieldset></dl></div>
      <bdo id='mLmJG'></bdo><ul id='mLmJG'></ul>
  1. <small id='mLmJG'></small><noframes id='mLmJG'>

      <tfoot id='mLmJG'></tfoot>

      <legend id='mLmJG'><style id='mLmJG'><dir id='mLmJG'><q id='mLmJG'></q></dir></style></legend>

      .Net Garbage 是否会收集未引用但具有正在工作的线程的对象?

      Will .Net Garbage Collect an object that#39;s not referenced, but has a thread that#39;s doing work?(.Net Garbage 是否会收集未引用但具有正在工作的线程的对象?)
          • <small id='sfO1i'></small><noframes id='sfO1i'>

            <tfoot id='sfO1i'></tfoot>

                <bdo id='sfO1i'></bdo><ul id='sfO1i'></ul>

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

                  <tbody id='sfO1i'></tbody>

                本文介绍了.Net Garbage 是否会收集未引用但具有正在工作的线程的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下代码(为了便于阅读而删减):

                I have the following code (cut down for readability):

                主类:

                public StartProcess()
                {
                    Thinker th = new Thinker();
                    th.DoneThinking += new Thinker.ProcessingFinished(ThinkerFinished);
                    th.StartThinking();
                }
                
                void ThinkerFinished()
                {
                    Console.WriteLine("Thinker finished");
                }
                

                思考者班:

                public class Thinker
                {
                    private System.Timers.Timer t;
                
                    public delegate void ProcessingFinished();
                    public event ProcessingFinished DoneThinking;
                
                    BackgroundWorker backgroundThread;
                
                    public Thinker() { }
                
                    public StartThinking()
                    {
                        t = new System.Timers.Timer(5000);    // 5 second timer
                        t.AutoReset = false;
                        t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                        t.Start();
                
                        // start a background thread to do the thinking
                        backgroundThread = new BackgroundWorker();
                        backgroundThread.DoWork += new DoWorkEventHandler(BgThread_DoWork);
                        backgroundThread.RunWorkerAsync();
                    }
                
                    void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
                    {
                        DoneThinking();
                    }
                
                    BgThread_DoWork(object sender, DoWorkEventArgs e)
                    {
                        // work in here should go for much less than 5 seconds
                        // it will die if it doesn't
                
                        t.Stop();
                        DoneThinking();
                    }
                }
                

                我最初预计会发生的是,主类中的事件处理程序会阻止 Thinker 被垃圾收集.

                What I originally expected to happen was that the event handler in the main class would prevent the Thinker from being garbage collected.

                显然不是这样.

                我现在想知道不管这个线程是否忙",是否会进行垃圾收集.换句话说,是否有可能在 5 秒超时到期之前被垃圾回收?

                I'm now wondering whether garbage collection will occur regardless of whether this thread is "busy" or not. In other words, is there a chance it will be garbage collected before the 5 second timeout has expired?

                换句话说,垃圾收集器是否有可能在我的思想者完成处理之前收集它?

                To put it another way, is it possible for the garbage collector to collect my Thinker before it's finished processing?

                推荐答案

                不,一个线程只要被引用就被认为是活跃的,并且任何正在运行的线程都被认为是被引用的(IIRC一个正在运行的线程注册它的堆栈作为 GC 根,该堆栈将引用该线程).

                No, a thread is considered live as long as it is referenced, and any thread that is running is considered to be referenced (IIRC a running thread registers its stack as a GC root, and that stack will reference the thread).

                也就是说我正在查看您的示例,但我不明白您认为线程是在哪里产生的?

                That said i'm looking at your example and i don't understand where you believe a thread is being spawned?

                这篇关于.Net Garbage 是否会收集未引用但具有正在工作的线程的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

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

                  <legend id='g20fS'><style id='g20fS'><dir id='g20fS'><q id='g20fS'></q></dir></style></legend><tfoot id='g20fS'></tfoot>

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

                          <bdo id='g20fS'></bdo><ul id='g20fS'></ul>
                            <tbody id='g20fS'></tbody>