-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ThreadProxyRenderTimer * Forgot IsBackground * Increase default stack size in ThreadProxyRenderTimer
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Avalonia.Metadata; | ||
|
||
namespace Avalonia.Rendering; | ||
|
||
[PrivateApi] | ||
public sealed class ThreadProxyRenderTimer : IRenderTimer | ||
{ | ||
private readonly IRenderTimer _inner; | ||
private readonly Stopwatch _stopwatch; | ||
private readonly Thread _timerThread; | ||
private readonly AutoResetEvent _autoResetEvent; | ||
private Action<TimeSpan>? _tick; | ||
private int _subscriberCount; | ||
private bool _registered; | ||
|
||
public ThreadProxyRenderTimer(IRenderTimer inner, int maxStackSize = 1 * 1024 * 1024) | ||
{ | ||
_inner = inner; | ||
_stopwatch = new Stopwatch(); | ||
_autoResetEvent = new AutoResetEvent(false); | ||
_timerThread = new Thread(RenderTimerThreadFunc, maxStackSize) { Name = "RenderTimerLoop", IsBackground = true }; | ||
} | ||
|
||
public event Action<TimeSpan> Tick | ||
{ | ||
add | ||
{ | ||
_tick += value; | ||
|
||
if (!_registered) | ||
{ | ||
_registered = true; | ||
_timerThread.Start(); | ||
} | ||
|
||
if (_subscriberCount++ == 0) | ||
{ | ||
_inner.Tick += InnerTick; | ||
} | ||
} | ||
|
||
remove | ||
{ | ||
if (--_subscriberCount == 0) | ||
{ | ||
_inner.Tick -= InnerTick; | ||
} | ||
|
||
_tick -= value; | ||
} | ||
} | ||
|
||
private void RenderTimerThreadFunc() | ||
{ | ||
while (_autoResetEvent.WaitOne()) | ||
{ | ||
_tick?.Invoke(_stopwatch.Elapsed); | ||
} | ||
} | ||
|
||
private void InnerTick(TimeSpan obj) | ||
{ | ||
_autoResetEvent.Set(); | ||
} | ||
|
||
public bool RunsInBackground => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters