C#: Implement disposable pattern and seal GodotSynchronizationContext
class and related
- `GodotSynchronizationContext` - Implements `IDisposable` to dispose of the disposable field `_queue`. - Makes the class sealed. - `GodotTaskScheduler` - Implements `IDisposable` to dispose of the disposable property `Context`. - Makes the class sealed. - `Dispatcher` - Dispose of previous `GodotTaskScheduler` instances before creating a new one.
This commit is contained in:
parent
9de0c73e45
commit
fc72604bef
@ -9,7 +9,10 @@ namespace Godot
|
||||
internal static GodotTaskScheduler DefaultGodotTaskScheduler;
|
||||
|
||||
internal static void InitializeDefaultGodotTaskScheduler()
|
||||
=> DefaultGodotTaskScheduler = new GodotTaskScheduler();
|
||||
{
|
||||
DefaultGodotTaskScheduler?.Dispose();
|
||||
DefaultGodotTaskScheduler = new GodotTaskScheduler();
|
||||
}
|
||||
|
||||
public static GodotSynchronizationContext SynchronizationContext => DefaultGodotTaskScheduler.Context;
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Godot
|
||||
{
|
||||
public class GodotSynchronizationContext : SynchronizationContext
|
||||
public sealed class GodotSynchronizationContext : SynchronizationContext, IDisposable
|
||||
{
|
||||
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> _queue =
|
||||
new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
|
||||
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> _queue = new();
|
||||
|
||||
public override void Post(SendOrPostCallback d, object state)
|
||||
{
|
||||
@ -24,5 +24,10 @@ namespace Godot
|
||||
workItem.Key(workItem.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_queue.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace Godot
|
||||
/// GodotTaskScheduler contains a linked list of tasks to perform as a queue. Methods
|
||||
/// within the class are used to control the queue and perform the contained tasks.
|
||||
/// </summary>
|
||||
public class GodotTaskScheduler : TaskScheduler
|
||||
public sealed class GodotTaskScheduler : TaskScheduler, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The current synchronization context.
|
||||
@ -108,5 +108,10 @@ namespace Godot
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Context.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user