一段代码部分看不明白,问一下大家什么意思?

dabobonimama 2010-01-11 05:41:10


  using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;


public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public StringBuilder requestData;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream streamResponse;
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
requestData = new StringBuilder("");
request = null;
streamResponse = null;
}
}

class HttpWebRequest_BeginGetResponse
{
public static ManualResetEvent allDone= new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
const int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout

// Abort the request if the timer fires.
private static void TimeoutCallback(object state, bool timedOut) {
if (timedOut) {
HttpWebRequest request = state as HttpWebRequest;
if (request != null) {
request.Abort();
}
}
}

static void Main()
{

try
{
// Create a HttpWebrequest object to the desired URL.
HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create("http://www.contoso.com");


/**
* If you are behind a firewall and you do not have your browser proxy setup
* you need to use the following proxy creation code.

// Create a proxy object.
WebProxy myProxy = new WebProxy();

// Associate a new Uri object to the _wProxy object, using the proxy address
// selected by the user.
myProxy.Address = new Uri("http://myproxy");


// Finally, initialize the Web request object proxy property with the _wProxy
// object.
myHttpWebRequest.Proxy=myProxy;
***/

// Create an instance of the RequestState and assign the previous myHttpWebRequest
// object to its request field.
RequestState myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest;


// Start the asynchronous request.
IAsyncResult result=
(IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

// this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
ThreadPool.RegisterWaitForSingleObject (result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myHttpWebRequest, DefaultTimeout, true);

// The response came in the allowed time. The work processing will happen in the
// callback function.
allDone.WaitOne();

// Release the HttpWebResponse resource.
myRequestState.response.Close();
}
catch(WebException e)
{
Console.WriteLine("\nMain Exception raised!");
Console.WriteLine("\nMessage:{0}",e.Message);
Console.WriteLine("\nStatus:{0}",e.Status);
Console.WriteLine("Press any key to continue..........");
}
catch(Exception e)
{
Console.WriteLine("\nMain Exception raised!");
Console.WriteLine("Source :{0} " , e.Source);
Console.WriteLine("Message :{0} " , e.Message);
Console.WriteLine("Press any key to continue..........");
Console.Read();
}
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest=myRequestState.request;
myRequestState.response = (HttpWebResponse) myHttpWebRequest.EndGetResponse(asynchronousResult);

// Read the response into a Stream object.
Stream responseStream = myRequestState.response.GetResponseStream();
myRequestState.streamResponse=responseStream;

// Begin the Reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
return;
}
catch(WebException e)
{
Console.WriteLine("\nRespCallback Exception raised!");
Console.WriteLine("\nMessage:{0}",e.Message);
Console.WriteLine("\nStatus:{0}",e.Status);
}
allDone.Set();
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try
{

RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.streamResponse;
int read = responseStream.EndRead( asyncResult );
// Read the HTML page and then print it to the console.
if (read > 0)
{
myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.BufferRead, 0, read));
IAsyncResult asynchronousResult = responseStream.BeginRead( myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
return;
}
else
{
Console.WriteLine("\nThe contents of the Html page are : ");
if(myRequestState.requestData.Length>1)
{
string stringContent;
stringContent = myRequestState.requestData.ToString();
Console.WriteLine(stringContent);
}
Console.WriteLine("Press any key to continue..........");
Console.ReadLine();

responseStream.Close();
}

}
catch(WebException e)
{
Console.WriteLine("\nReadCallBack Exception raised!");
Console.WriteLine("\nMessage:{0}",e.Message);
Console.WriteLine("\nStatus:{0}",e.Status);
}
allDone.Set();

}


这句代码IAsyncResult result=
(IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);
我们知道调用了RespCallback这个函数.
现在我想问一下这个函数的传入参数是什么呀?即private static void ReadCallBack(IAsyncResult asyncResult)
中的asyncResult这个实际传入参数是什么?是上面的myRequestState吗??

不太理解望高手指点下,谢谢!!
...全文
112 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengjian_428 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 dabobonimama 的回复:]
引用 12 楼 fengjian_428 的回复:
什么时候执行是BeginGetResponse的事 你只是传了个委托给它


你的意思这里不立刻执行,还要事件去触发??
[/Quote]
BeginGetResponse获得了你给它的函数指针 然后在自己内部去调用 至于什么时候调用是它的事
cena_jin 2010-01-12
  • 打赏
  • 举报
回复
吼吼!!!
fwacky 2010-01-12
  • 打赏
  • 举报
回复
有点深度!
风骑士之怒 2010-01-12
  • 打赏
  • 举报
回复
up
dabobonimama 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fengjian_428 的回复:]
什么时候执行是BeginGetResponse的事 你只是传了个委托给它
[/Quote]

你的意思这里不立刻执行,还要事件去触发??
fengjian_428 2010-01-12
  • 打赏
  • 举报
回复
什么时候执行是BeginGetResponse的事 你只是传了个委托给它
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
上面这个是不是走到IAsyncResult result=
(IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);
这句就立刻执行了RespCallback函数呢,应为这里好像没有事件订阅的概念呢
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
哦,我理解下先,看样子我是对委托还没完全理解
我只是对+= new EventHandler()这样知道了,即先定义委托,然后给事件关联,最后调用事件即触发委托.
对上面这种情况还不是太了解呢
fengjian_428 2010-01-11
  • 打赏
  • 举报
回复
委托是一个封装的函数指针 只能与函数直接关联 而函数的参数要额外传
可能是因为这个原因吧

用习惯了之后就不觉得怪了。
fengjian_428 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dabobonimama 的回复:]
引用 1 楼 fengjian_428 的回复:
ReadCallBack的参数是myRequestState没错

怎么理解的?是BeginGetResponse函数有这样一个功能?第2个参数传递给第一个参数了?
[/Quote]
是啊 他就是这样定义的 线程池不也是这样吗
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
为什么不设计成IAsyncResult result=
(IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback(myRequestState));
这样呢?不是更好理解吗
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fengjian_428 的回复:]
AsyncCallback是一个带IAsyncResult参数的委托 new AsyncCallback(RespCallback)使委托对应到RespCallback方法。给委托传参就在后面写,实际就是给RespCallback方法传参了
[/Quote]
哦,那么是不是可以这样理解.
当一个方法需要用AsyncCallback当作参数时候,如需要给new AsyncCallback(RespCallback)对应方法传参数,那么需要再另外添加一个参数.

感觉AsyncCallback这个委托好神奇呀这样
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
还是AsyncCallback委托具有这样的功能?
fengjian_428 2010-01-11
  • 打赏
  • 举报
回复
AsyncCallback是一个带IAsyncResult参数的委托 new AsyncCallback(RespCallback)使委托对应到RespCallback方法。给委托传参就在后面写,实际就是给RespCallback方法传参了
Jack2013tong 2010-01-11
  • 打赏
  • 举报
回复
dabobonimama 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fengjian_428 的回复:]
ReadCallBack的参数是myRequestState没错
[/Quote]
怎么理解的?是BeginGetResponse函数有这样一个功能?第2个参数传递给第一个参数了?
fengjian_428 2010-01-11
  • 打赏
  • 举报
回复
ReadCallBack的参数是myRequestState没错

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧