use Task.Delay instead of Thread.Sleep

pull/3895/head
2dust 2 years ago
parent abe484b0df
commit 566f056149

@ -2,7 +2,6 @@
using System.IO; using System.IO;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace PacLib; namespace PacLib;
@ -50,7 +49,7 @@ public class PacHandler
_tcpListener = TcpListener.Create(_pacPort); _tcpListener = TcpListener.Create(_pacPort);
_isRunning = true; _isRunning = true;
_tcpListener.Start(); _tcpListener.Start();
Task.Factory.StartNew(() => Task.Factory.StartNew(async () =>
{ {
while (_isRunning) while (_isRunning)
{ {
@ -58,25 +57,25 @@ public class PacHandler
{ {
if (!_tcpListener.Pending()) if (!_tcpListener.Pending())
{ {
Thread.Sleep(10); await Task.Delay(10);
continue; continue;
} }
var client = _tcpListener.AcceptTcpClient(); var client = _tcpListener.AcceptTcpClient();
Task.Run(() => await Task.Run(() =>
{ {
var stream = client.GetStream(); var stream = client.GetStream();
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("HTTP/1.0 200 OK"); sb.AppendLine("HTTP/1.0 200 OK");
sb.AppendLine("Content-type:application/x-ns-proxy-autoconfig"); sb.AppendLine("Content-type:application/x-ns-proxy-autoconfig");
sb.AppendLine("Connection:close"); sb.AppendLine("Connection:close");
sb.AppendLine("Content-Length:" + Encoding.UTF8.GetByteCount(_pacText)); sb.AppendLine("Content-Length:" + Encoding.UTF8.GetByteCount(_pacText));
sb.AppendLine(); sb.AppendLine();
sb.Append(_pacText); sb.Append(_pacText);
var content = Encoding.UTF8.GetBytes(sb.ToString()); var content = Encoding.UTF8.GetBytes(sb.ToString());
stream.Write(content, 0, content.Length); stream.Write(content, 0, content.Length);
stream.Flush(); stream.Flush();
}); });
} }
catch (Exception e) catch (Exception e)
{ {

Loading…
Cancel
Save