public void Run() { while (isRunning) { _ = ThreadPool.QueueUserWorkItem((c) => { var ctx = c as HttpListenerContext; bool atsAgent = ctx.Request.UserAgent.Equals(ATS_USER_AGENT); try { SendResponse(ctx); } catch (Exception e) { DesktopRequest req = new DesktopRequest(-99, atsAgent, "error -> " + e.StackTrace.ToString()); isRunning = req.Execute(ctx); } }, listener.GetContext()); } }