积极答复者
一个异步回调的问题盼高手解惑

问题
-
class Program { static byte[] buffer; static void Main(string[] args) { Thread.CurrentThread.Name = "main thread"; Console.WriteLine(Thread.CurrentThread.ManagedThreadId); using (System.IO.FileStream fs = new System.IO.FileStream("read.txt",System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.None,4096,true)) { buffer= new byte[fs.Length]; fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs); } Console.ReadLine(); } static void HandleCallback(IAsyncResult ar) { using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream) { int numbers= fs.EndRead(ar); Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer,0,buffer.Length)); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } } }
代码很简单,在运行以上代码前,您判断一下,fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs);这一行中最后的fs出了using的作用域后,回调函数中的
using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream)
得到的fs会不会是null?
按理说fs出了using后,即销毁了这个对象,但是实际运行时,在回调函数中,可以拿到非null的fs.实在有些不解?
是不是因为垃圾回收会在下次回收时才销毁这个对象呢?盼高手解惑.谢谢.
答案
-
class Program { static byte[] buffer; static void Main(string[] args) { Thread.CurrentThread.Name = "main thread"; Console.WriteLine(Thread.CurrentThread.ManagedThreadId); using (System.IO.FileStream fs = new System.IO.FileStream("read.txt",System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.None,4096,true)) { buffer= new byte[fs.Length]; fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs); } Console.ReadLine(); } static void HandleCallback(IAsyncResult ar) { using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream) { int numbers= fs.EndRead(ar); Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer,0,buffer.Length)); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } } }
代码很简单,在运行以上代码前,您判断一下,fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs);这一行中最后的fs出了using的作用域后,回调函数中的
using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream)
得到的fs会不会是null?
按理说fs出了using后,即销毁了这个对象,但是实际运行时,在回调函数中,可以拿到非null的fs.实在有些不解?
是不是因为垃圾回收会在下次回收时才销毁这个对象呢?盼高手解惑.谢谢.
fs对象的回收,不一定在出作用域以后,取决于GC的执行时间和策略。
dispose()方法的调用,只是立即释放了使用的非托管资源,而这个对象的托管资源可能还在内存中。CLR viz C#提到过一个对象复苏的问题。
我猜测这里有个时间差。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
【老徐的博客】:http://www.cnblogs.com/frank_xl/
- 已标记为答案 ThankfulHeartModerator 2010年9月24日 6:38
全部回复
-
- 已合并 Sheng Jiang 蒋晟Moderator 2010年9月19日 14:52
-
似乎好像没有出定义域啊:
using (System.IO.FileStream fs = new System.IO.FileStream("read.txt",System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.None,4096,true))
{
buffer= new byte[fs.Length];
fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs);
}
Console.ReadLine(); //此处才跳出using块。 -
class Program { static byte[] buffer; static void Main(string[] args) { Thread.CurrentThread.Name = "main thread"; Console.WriteLine(Thread.CurrentThread.ManagedThreadId); using (System.IO.FileStream fs = new System.IO.FileStream("read.txt",System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.None,4096,true)) { buffer= new byte[fs.Length]; fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs); } Console.ReadLine(); } static void HandleCallback(IAsyncResult ar) { using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream) { int numbers= fs.EndRead(ar); Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer,0,buffer.Length)); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } } }
代码很简单,在运行以上代码前,您判断一下,fs.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(HandleCallback), fs);这一行中最后的fs出了using的作用域后,回调函数中的
using (System.IO.FileStream fs = ar.AsyncState as System.IO.FileStream)
得到的fs会不会是null?
按理说fs出了using后,即销毁了这个对象,但是实际运行时,在回调函数中,可以拿到非null的fs.实在有些不解?
是不是因为垃圾回收会在下次回收时才销毁这个对象呢?盼高手解惑.谢谢.
fs对象的回收,不一定在出作用域以后,取决于GC的执行时间和策略。
dispose()方法的调用,只是立即释放了使用的非托管资源,而这个对象的托管资源可能还在内存中。CLR viz C#提到过一个对象复苏的问题。
我猜测这里有个时间差。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
【老徐的博客】:http://www.cnblogs.com/frank_xl/
- 已标记为答案 ThankfulHeartModerator 2010年9月24日 6:38