Answer the question
In order to leave comments, you need to log in
How to implement asynchronous listening on HttpListener C#?
Tell me how to implement listening to http requests in the following code without slowing down the work of the main program?
HttpListener listener = new HttpListener();
StringBuilder log = new StringBuilder();
string path;
private async void button1_Click(object sender, EventArgs e)
{
listener.Prefixes.Add("http://localhost:8080/");
listener.Start();
//listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
if (listener.IsListening)
{
button1.Enabled = false;
log.Append("Start server...");
richTextBox1.Text= log.ToString();
}
while (true)
try
{
HttpListenerContext request = listener.GetContext();
ThreadPool.QueueUserWorkItem(ProcessRequest, request);
}
catch (HttpListenerException) { break; }
catch (InvalidOperationException) { break; }
}
Answer the question
In order to leave comments, you need to log in
Perhaps in your case you can do this:
// Тут запускаем прослушку
Task.Factrory.StartNew(()=>{
// Тут уже обрабатываем запросы
while (true)
try
{
HttpListenerContext request = listener.GetContext();
ThreadPool.QueueUserWorkItem(ProcessRequest, request);
}
catch (HttpListenerException) { break; }
catch (InvalidOperationException) { break; } });
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question