• <legend id='4h09S'><style id='4h09S'><dir id='4h09S'><q id='4h09S'></q></dir></style></legend>
    • <bdo id='4h09S'></bdo><ul id='4h09S'></ul>

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

      1. <small id='4h09S'></small><noframes id='4h09S'>

        垃圾收集器是否会在 .NET 中的异步调用期间销毁临时未引用的对象?

        时间:2023-09-13
      2. <legend id='qd2wA'><style id='qd2wA'><dir id='qd2wA'><q id='qd2wA'></q></dir></style></legend>
          <tbody id='qd2wA'></tbody>
        • <tfoot id='qd2wA'></tfoot>

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

                  <bdo id='qd2wA'></bdo><ul id='qd2wA'></ul>
                  本文介绍了垃圾收集器是否会在 .NET 中的异步调用期间销毁临时未引用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  想象一下,我将在 .NET 中进行异步调用,即 HttpWebRequest.BeginGetResponse,并且 HttpWebRequest 对象没有在更广泛的范围内被引用.垃圾收集器会破坏它并引起问题吗?

                  Imagine that I will make an async call in .NET, i.e. HttpWebRequest.BeginGetResponse, and the HttpWebRequest object isn't referenced at a broader scope. Will the Garbage Collector destroy it and cause problems?

                  示例代码:

                  using System;
                  using System.Net;
                  
                  public class AsyncHttpWebRequest
                  {
                      void Main()
                      {
                          var Request = HttpWebRequest.Create("http://www.contoso.com");
                          var result = Request.BeginGetResponse(GetResponseCallback, null);
                      }
                  
                      private void GetResponseCallback(IAsyncResult AsyncResult)
                      {
                          // Do Something..
                      }
                  }
                  

                  替代版本(请求作为 AsyncState 传递):

                  Alternate version (with the request being passed as an AsyncState):

                  using System;
                  using System.Net;
                  
                  public class AsyncHttpWebRequest
                  {
                      void Main()
                      {
                          var Request = HttpWebRequest.Create("http://www.contoso.com");
                          var result = Request.BeginGetResponse(GetResponseCallback, Request);
                      }
                  
                      private void GetResponseCallback(IAsyncResult AsyncResult)
                      {
                          // Do Something..
                      }
                  }
                  

                  推荐答案

                  如果任何活动线程包含对它的引用,或者如果它被静态引用(直接或间接在两者中例).

                  An object is considered alive and non-eligible for garbage collection if any live thread contains a reference to it, or if it's referenced statically (directly or indirectly in both cases).

                  在这两个示例中,异步 API 都会保留对您请求的引用(在提交异步 IO 操作的线程池中),因此在完成之前不会被垃圾收集.

                  In both examples the async API keeps a reference to your request (within the thread pool where async IO operations are lodged) and so it won't be garbage collected until it completes.

                  这篇关于垃圾收集器是否会在 .NET 中的异步调用期间销毁临时未引用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:正在收集回调代表? 下一篇:垃圾收集何时在 C# 中触发?

                  相关文章

                • <legend id='HsTPO'><style id='HsTPO'><dir id='HsTPO'><q id='HsTPO'></q></dir></style></legend>

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

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