I am looking to build an httpListener into a small server app. Whilst reading up on it, i encountered the following code snippet in this question on stackoverflow
Public Class HTTPServer
Shared Listener As HttpListener = New HttpListener
Public Shared Sub Start()
ServicePointManager.DefaultConnectionLimit = 500
ServicePointManager.Expect100Continue = False
ServicePointManager.MaxServicePoints = 500
Listener.Prefixes.Add("http://localhost/")
Listener.Start()
For i As Integer = 1 To (System.Environment.ProcessorCount * 2)
Dim NewThread As New System.Threading.Thread(AddressOf ListenerThread)
NewThread.Priority = ThreadPriority.Normal
NewThread.IsBackground = True
NewThread.Start()
Next
End Sub
Private Shared Sub ListenerThread()
Dim SyncResult As IAsyncResult
While True
SyncResult = Listener.BeginGetContext(New AsyncCallback(AddressOf ListenerCallback), Listener)
SyncResult.AsyncWaitHandle.WaitOne()
End While
End Sub
Private Shared Sub ListenerCallback(ByVal StateObject As IAsyncResult)
Dim Listener As HttpListener = DirectCast(StateObject.AsyncState, HttpListener)
Dim Context As HttpListenerContext = Listener.EndGetContext(StateObject)
Dim Request As HttpListenerRequest = Context.Request
Dim Response As HttpListenerResponse = Context.Response
Dim ResponseString As String = "OK"
Dim Buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(ResponseString)
Response.ContentLength64 = Buffer.Length
Dim OutputStream As System.IO.Stream = Response.OutputStream
OutputStream.Write(Buffer, 0, Buffer.Length)
OutputStream.Close()
OutputStream.Dispose()
End Sub
End Class
Which seemed pretty simple, and seems much like the msdn example. However when testing it in a dummy project, i found a few things that confused me, for example UI objects can be accessed directly from the callback sub, which i thought should cause a cross threading exception.
To clarify, i modified this code slightly to run within the main form of a simple winforms project.
It seems i do not fully understand the code, for example AsyncWaitHandle.WaitOne() is completely new to me.
Could someone briefly walk me through this snippet please? Any help appreciated.
This snippet code looks like:
Because it called SyncResult.AsyncWaitHandle.WaitOne() to block that thread, it will get the same result with Synchronously call.
In my opinion, it just returns a simple OK response, so we even don’t need any other threads. I will show you the code soon (My code will not work very well if we have heavy operations, and please don’t do this in form application.)
In C#5 and Net 4.5, we have sync method, it will much easier:
For your another question about “cross threading exception” for UI, you are right, the created new Thread will have a null value for SynchronizationContext.Current because it’s a thread pool thread, we need do a post() in form’s SynchronizationContext. Here are more information http://msdn.microsoft.com/en-us/magazine/gg598924.aspx and http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx.