Answered by:
asynchronous function callback

Question
-
Hello everyone,
Here is my code for an HttpService. My question is, if there is no coming client request, both (1) and (2) wil be blocked and when there is a coming request,1. the the callback function provided as the 1st parameter of the BeginGetContext function will be called;
2. and also for the handle wait on (result.AsyncWaitHandle.WaitOne()) will be signalled.Are there any sequences for (1) and (2)? i.e. for asynchronous call, signal is set before callback is called or vice versa?
[Code]
public class TestHttpServer
{
// listening HTTP port
private int _Port = 0;// internal wrapped HTTP listener
private HttpListener _Server = new HttpListener();private Service1 _manager;
public TestHttpServer (Service1 manager)
{
_manager = manager;
}public int ListenPort
{
get
{
return _Port;
}
set
{
_Port = value;
}
}public void StartListen()
{
try
{
IAsyncResult result;
_Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
_Server.Start();
while (true)
{
result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
result.AsyncWaitHandle.WaitOne();
}
}
// any exceptions are not expected
// catch InvalidOperationException during service stop
// [System.InvalidOperationException] = {"Please call the Start() method before calling this method."}
catch (Exception ex)
{
throw ex;
}
}public void Stop(bool isTerminate)
{
_Server.Stop();
}// callback function when there is HTTP request received
private void HttpCallback(IAsyncResult result)
{
HttpListenerContext context = _Server.EndGetContext(result);
HandleRequest(context);
}// find matched URL HTTP request handler and invoke related handler
private void HandleRequest(HttpListenerContext context)
{
string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Close();
}
}
[/Code]
thanks in advance,
George- Moved by jack 321 Thursday, June 12, 2008 8:22 AM off topic for C# general
Tuesday, June 10, 2008 3:15 AM
Answers
-
Discussion in .NET Framework Networking and Communication
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
Thursday, June 12, 2008 8:23 AM -
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
Thursday, May 10, 2012 6:39 AM
All replies
-
Discussion in .NET Framework Networking and Communication
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
Thursday, June 12, 2008 8:23 AM -
- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, May 10, 2012 6:39 AM
Thursday, May 10, 2012 6:39 AM