Skip to content

Commit

Permalink
fix(RandomWalk): do not use Thread.Abort
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed May 21, 2019
1 parent 34d534d commit e5674de
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/RandomWalk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class RandomWalk : IService
static ILog log = LogManager.GetLogger(typeof(RandomWalk));
Random rng = new Random();
Thread thread;
CancellationTokenSource cancel;

/// <summary>
/// The Distributed Hash Table to query.
Expand Down Expand Up @@ -58,6 +59,7 @@ public Task StartAsync()
{
IsBackground = true
};
cancel = new CancellationTokenSource();
thread.Start();
log.Debug("started");

Expand All @@ -69,7 +71,9 @@ public Task StartAsync()
/// </summary>
public Task StopAsync()
{
thread?.Abort();
cancel?.Cancel();
cancel?.Dispose();
cancel = null;
thread = null;

log.Debug("stopped");
Expand Down Expand Up @@ -106,7 +110,8 @@ void RunQuery()
var id = MultiHash.ComputeHash(x);

// Run the query for a while.
using (var cts = new CancellationTokenSource(QueryTime))
using (var timeout = new CancellationTokenSource(QueryTime))
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancel.Token))
{
var _ = Dht.FindPeerAsync(id, cts.Token).Result;
}
Expand Down

0 comments on commit e5674de

Please sign in to comment.