Csharp/C Sharp by API/System.Threading/ThreadPool
ThreadPool.QueueUserWorkItem
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
public static void Main()
{
ThreadPool.QueueUserWorkItem(delegate {
Console.WriteLine(Assembly.GetCallingAssembly().FullName);
});
Thread.Sleep(100);
}
}
ThreadPool.RegisterWaitForSingleObject
using System;
using System.Threading;
class MainClass
{
private static void EventHandler(object state, bool timedout)
{
Console.WriteLine("timedout:"+timedout);
Console.WriteLine("state:"+state);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff"));
}
public static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
string state = "AutoResetEvent signaled.";
RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(
autoEvent, EventHandler, state, 3000, false);
Thread.Sleep(5000);
autoEvent.Set();
Console.WriteLine("Unregistering wait operation.");
handle.Unregister(null);
}
}
ThreadPool.SetMaxThreads
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
class WebServer {
HttpListener myListener;
string _baseFolder;
public WebServer(string uriPrefix, string baseFolder) {
System.Threading.ThreadPool.SetMaxThreads(50, 1000);
System.Threading.ThreadPool.SetMinThreads(50, 50);
myListener = new HttpListener();
myListener.Prefixes.Add(uriPrefix);
_baseFolder = baseFolder;
}
public void Start() {
myListener.Start();
while (true)
try {
HttpListenerContext request = myListener.GetContext();
ThreadPool.QueueUserWorkItem(ProcessRequest, request);
} catch (HttpListenerException) { break; }
catch (InvalidOperationException) { break; }
}
public void Stop() {
myListener.Stop();
}
void ProcessRequest(object listenerContext) {
try {
var context = (HttpListenerContext)listenerContext;
string filename = Path.GetFileName(context.Request.RawUrl);
string path = Path.rubine(_baseFolder, filename);
byte[] msg;
if (!File.Exists(path)) {
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
msg = Encoding.UTF8.GetBytes("Sorry, that page does not exist");
} else {
context.Response.StatusCode = (int)HttpStatusCode.OK;
msg = File.ReadAllBytes(path);
}
context.Response.ContentLength64 = msg.Length;
using (Stream s = context.Response.OutputStream)
s.Write(msg, 0, msg.Length);
} catch (Exception ex) { Console.WriteLine("Request error: " + ex); }
}
}