Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[StoppingMode] Clean up WaitHandleGroup a bit.

Use WaitHandle.WaitAll () instead of the custom crap which may or may not
work properly in all cases.
  • Loading branch information...
commit dd7545a7081090bac8fe0e6c77c133bde0096269 1 parent 33da612
@alanmcgovern alanmcgovern authored
View
2  src/MonoTorrent/MonoTorrent.Client/Modes/StoppingMode.cs
@@ -7,7 +7,7 @@ namespace MonoTorrent.Client
{
class StoppingMode : Mode
{
- WaitHandleGroup handle = new WaitHandleGroup("Global");
+ WaitHandleGroup handle = new WaitHandleGroup();
public override TorrentState State
{
View
64 src/MonoTorrent/MonoTorrent.Client/WaitHandleGroup.cs
@@ -8,74 +8,74 @@ namespace MonoTorrent.Client
class WaitHandleGroup : WaitHandle
{
private List<WaitHandle> handles;
- private string name;
+ private List<string> names;
- public string Name
+ public WaitHandleGroup()
{
- get { return name; }
+ handles = new List<WaitHandle>();
+ names = new List<string> ();
}
- public WaitHandleGroup(string name)
+ public void AddHandle(WaitHandle handle, string name)
{
- this.name = name;
- handles = new List<WaitHandle>();
+ handles.Add (handle);
+ names.Add (name);
}
- public void AddHandle(WaitHandle handle, string name)
+ public override void Close ()
{
- WaitHandleGroup h = new WaitHandleGroup(name);
- h.handles.Add(handle);
- handles.Add(h);
+ for (int i = 0; i < handles.Count; i++)
+ handles [i].Close ();
}
public override bool WaitOne()
{
if (handles.Count == 0)
return true;
+ return WaitHandle.WaitAll (handles.ToArray ());
+ }
- for (int i = 0; i < handles.Count; i++)
- handles[i].WaitOne();
+ public override bool WaitOne (int millisecondsTimeout)
+ {
+ if (handles.Count == 0)
+ return true;
+ return WaitHandle.WaitAll (handles.ToArray (), millisecondsTimeout);
+ }
- return true;
+ public override bool WaitOne (TimeSpan timeout)
+ {
+ if (handles.Count == 0)
+ return true;
+ return WaitHandle.WaitAll (handles.ToArray (), timeout);
}
public override bool WaitOne(int millisecondsTimeout, bool exitContext)
{
- return WaitOne(TimeSpan.FromMilliseconds(millisecondsTimeout), exitContext);
+ if (handles.Count == 0)
+ return true;
+ return WaitHandle.WaitAll (handles.ToArray (), millisecondsTimeout, exitContext);
}
public override bool WaitOne(TimeSpan timeout, bool exitContext)
{
if (handles.Count == 0)
return true;
-
- for (int i = 0; i < handles.Count; i++)
- {
- int startTime = Environment.TickCount;
-
- if (!handles[i].WaitOne(timeout, exitContext))
- return false;
-
- timeout.Subtract(TimeSpan.FromMilliseconds(Environment.TickCount - startTime));
- if (timeout.TotalMilliseconds < 0)
- return false;
- }
-
- return true;
+ return WaitHandle.WaitAll (handles.ToArray (), timeout, exitContext);
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
- foreach (WaitHandle h in handles)
+ for (int i = 0; i < handles.Count; i ++)
{
- sb.Append("WaitHandle from: ");
- sb.Append(((WaitHandleGroup)h).name);
+ sb.Append("WaitHandle: ");
+ sb.Append(names [i]);
sb.Append(". State: ");
- sb.Append(h.WaitOne(0, false) ? "Signalled" : "Unsignalled");
+ sb.Append(handles [i].WaitOne(0) ? "Signalled" : "Unsignalled");
sb.AppendLine();
}
return sb.ToString();
}
}
}
+
Please sign in to comment.
Something went wrong with that request. Please try again.