我对http侦听器有几个问题。首先,在异步backgroundWorker任务中运行http侦听器是一种好习惯吗?还是应该使用线程或任务?
private void Main_Load(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8000/");
listener.Prefixes.Add("http://127.0.0.1:8000/");
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
listener.Start();
while (true)
{
var result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
result.AsyncWaitHandle.WaitOne();
}
}
private void ListenerCallback(IAsyncResult result)
{
var context = listener.EndGetContext(result);
}
第二个问题是我的示例权利,可以同时接受多个客户请求吗?