想象一下,我将在.NET中进行异步调用,即HttpWebRequest.BeginGetResponse,并且HttpWebRequest对象在更广泛的范围内不被引用.垃圾收集器会毁灭它并造成问题吗?
示例代码:
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传递):
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.. } }