@@ -42,11 +42,17 @@ public class RedisPubSubServer : IRedisPubSubServer
4242 private int status ;
4343 private Thread bgThread ; //Subscription controller thread
4444 private long bgThreadCount = 0 ;
45- private int autoRestart = YES ;
4645
4746 private const int NO = 0 ;
4847 private const int YES = 1 ;
4948
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+
5056 public DateTime CurrentServerTime
5157 {
5258 get { return new DateTime ( serverTimeAtStart . Ticks + startedAt . ElapsedTicks , DateTimeKind . Utc ) ; }
@@ -74,7 +80,7 @@ public RedisPubSubServer(IRedisClientsManager clientsManager, params string[] ch
7480
7581 public IRedisPubSubServer Start ( )
7682 {
77- Interlocked . CompareExchange ( ref autoRestart , 0 , autoRestart ) ;
83+ AutoRestart = true ;
7884
7985 if ( Interlocked . CompareExchange ( ref status , 0 , 0 ) == Status . Started )
8086 {
@@ -287,8 +293,7 @@ private void RunLoop()
287293 this . OnError ( ex ) ;
288294 }
289295
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 )
292297 {
293298 if ( KeepAliveRetryAfterMs != null )
294299 Thread . Sleep ( KeepAliveRetryAfterMs . Value ) ;
@@ -299,7 +304,12 @@ private void RunLoop()
299304
300305 public void Stop ( )
301306 {
302- Interlocked . CompareExchange ( ref autoRestart , NO , autoRestart ) ;
307+ Stop ( shouldRestart : false ) ;
308+ }
309+
310+ private void Stop ( bool shouldRestart )
311+ {
312+ AutoRestart = shouldRestart ;
303313
304314 if ( Interlocked . CompareExchange ( ref status , 0 , 0 ) == Status . Disposed )
305315 throw new ObjectDisposedException ( "RedisPubSubServer has been disposed" ) ;
@@ -384,8 +394,7 @@ void HandleUnSubscribe(string channel)
384394
385395 public void Restart ( )
386396 {
387- Stop ( ) ;
388- Interlocked . CompareExchange ( ref autoRestart , YES , autoRestart ) ;
397+ Stop ( shouldRestart : true ) ;
389398 }
390399
391400 private void KillBgThreadIfExists ( )
0 commit comments