@@ -42,11 +42,17 @@ public class RedisPubSubServer : IRedisPubSubServer
42
42
private int status ;
43
43
private Thread bgThread ; //Subscription controller thread
44
44
private long bgThreadCount = 0 ;
45
- private int autoRestart = YES ;
46
45
47
46
private const int NO = 0 ;
48
47
private const int YES = 1 ;
49
48
49
+ private int autoRestart = YES ;
50
+ public bool AutoRestart
51
+ {
52
+ get { return Interlocked . CompareExchange ( ref autoRestart , 0 , 0 ) == YES ; }
53
+ set { Interlocked . CompareExchange ( ref autoRestart , value ? YES : NO , autoRestart ) ; }
54
+ }
55
+
50
56
public DateTime CurrentServerTime
51
57
{
52
58
get { return new DateTime ( serverTimeAtStart . Ticks + startedAt . ElapsedTicks , DateTimeKind . Utc ) ; }
@@ -74,7 +80,7 @@ public RedisPubSubServer(IRedisClientsManager clientsManager, params string[] ch
74
80
75
81
public IRedisPubSubServer Start ( )
76
82
{
77
- Interlocked . CompareExchange ( ref autoRestart , 0 , autoRestart ) ;
83
+ AutoRestart = true ;
78
84
79
85
if ( Interlocked . CompareExchange ( ref status , 0 , 0 ) == Status . Started )
80
86
{
@@ -287,8 +293,7 @@ private void RunLoop()
287
293
this . OnError ( ex ) ;
288
294
}
289
295
290
- if ( Interlocked . CompareExchange ( ref autoRestart , 0 , 0 ) == YES
291
- && Interlocked . CompareExchange ( ref status , 0 , 0 ) != Status . Disposed )
296
+ if ( AutoRestart && Interlocked . CompareExchange ( ref status , 0 , 0 ) != Status . Disposed )
292
297
{
293
298
if ( KeepAliveRetryAfterMs != null )
294
299
Thread . Sleep ( KeepAliveRetryAfterMs . Value ) ;
@@ -299,7 +304,12 @@ private void RunLoop()
299
304
300
305
public void Stop ( )
301
306
{
302
- Interlocked . CompareExchange ( ref autoRestart , NO , autoRestart ) ;
307
+ Stop ( shouldRestart : false ) ;
308
+ }
309
+
310
+ private void Stop ( bool shouldRestart )
311
+ {
312
+ AutoRestart = shouldRestart ;
303
313
304
314
if ( Interlocked . CompareExchange ( ref status , 0 , 0 ) == Status . Disposed )
305
315
throw new ObjectDisposedException ( "RedisPubSubServer has been disposed" ) ;
@@ -384,8 +394,7 @@ void HandleUnSubscribe(string channel)
384
394
385
395
public void Restart ( )
386
396
{
387
- Stop ( ) ;
388
- Interlocked . CompareExchange ( ref autoRestart , YES , autoRestart ) ;
397
+ Stop ( shouldRestart : true ) ;
389
398
}
390
399
391
400
private void KillBgThreadIfExists ( )
0 commit comments