Changed Listener.Block to not use as much CPU usage, at least not visually in the task manager.

This commit is contained in:
MattMo 2023-03-01 12:22:23 -08:00
parent f882a74c6d
commit 662bd03ddc
2 changed files with 6 additions and 11 deletions

View File

@ -70,14 +70,6 @@ namespace MontoyaTech.Rest.Net.Example
Console.WriteLine($"Rest api server running at {listener.BaseUrl}"); Console.WriteLine($"Rest api server running at {listener.BaseUrl}");
var client = new Client(listener.BaseUrl);
var result = client.Auth.Get();
StaticClient.Init(listener.BaseUrl);
var result2 = StaticClient.Auth.Get();
listener.Block(); listener.Block();
} }

View File

@ -215,9 +215,12 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
public void Block() public void Block()
{ {
while (this.HttpListener != null && Thread.CurrentThread.ThreadState != ThreadState.AbortRequested && Thread.CurrentThread.ThreadState != ThreadState.Aborted) try
if (!Thread.Yield()) {
Thread.Sleep(1000); while (this.HttpListener != null && this.HttpListener.IsListening && Thread.CurrentThread.ThreadState != ThreadState.AbortRequested && Thread.CurrentThread.ThreadState != ThreadState.Aborted)
Thread.Sleep(100);
}
catch { }
} }
/// <summary> /// <summary>