ThreadSafeDisposable - add name property and check_not_disposed method
This commit is contained in:
1 parent
9b67ce7a43
commit
38f5f24952
1 file changed
+10
-1
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -138,13 +139,21 @@ namespace mbts
|
||||
class ThreadSafeDisposable : IDisposable {
|
||||
int _state = 0;
|
||||
readonly Action _dispose_once;
|
||||
readonly string _name;
|
||||
|
||||
public ThreadSafeDisposable(Action dispose_once) =>
|
||||
public ThreadSafeDisposable(Action dispose_once, [CallerMemberName]string name = "Anonymous") {
|
||||
_name = name;
|
||||
_dispose_once = dispose_once;
|
||||
}
|
||||
|
||||
public bool has_dispose_started => _state > 0;
|
||||
public bool has_dispose_finished { get; private set; }
|
||||
|
||||
public void check_not_disposed() {
|
||||
if (has_dispose_finished)
|
||||
throw new ObjectDisposedException($"{_name}'s {nameof(ThreadSafeDisposable)}");
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if (Interlocked.Increment(ref _state) == 1) {
|
||||
_dispose_once();
|
||||
|
||||
Reference in new issue
Block a user