Bug #357

avatar

TorrentManager.Stop() seems to be asynchronous

Added by Nik Ivanov 152 days ago. Updated 150 days ago.

Status:New Start:03/01/2010
Priority:Normal Due date:
Assigned to:- % Done:

0%

Category:- Spent time: -
Target version:-
Votes: 0

Description

I'm trying to download a file from a multi-file torrent using MonoTorrent. I set the priority of the file to Immediate while all other files are Do Not Download. I monitor the file progress, and once it reaches 100%, I call:

torrentManager.Stop();
torrentEngine.Unregister(torrentManager);

The second line throws an exception:
Exception in mainloop
The manager must be stopped before it can be unregistered
at MonoTorrent.Client.MainLoop.QueueWait(DelegateTask t)
at MonoTorrent.Client.MainLoop.QueueWait(MainLoopTask task)
at MonoTorrent.Client.ClientEngine.Unregister(TorrentManager manager)

Is there a way to synchronously stop a torrent manager, so I can unregister it?

History

Updated by Nik Ivanov 152 days ago

avatar

Forgot to mention:
This happens in MonoTorrent v0.80, release build.

Updated by Nik Ivanov 150 days ago

avatar

The following workaround seems to work at simulating Stop function being synchronous:
This is part of the function where you want to stop and unload the manager:

torrentManager.TorrentStateChanged += new EventHandler<TorrentStateChangedEventArgs>(torrentManager_TorrentStateChanged);

torrentManager.Stop();
lock (ManagerUnloadedLock) {
Monitor.Wait(ManagerUnloadedLock);
}

This is the event handler:

private object ManagerUnloadedLock = new object();
void torrentManager_TorrentStateChanged(object sender, TorrentStateChangedEventArgs e) {
if (e.NewState == TorrentState.Stopped) {
try {
torrentEngine.Unregister(e.TorrentManager);
e.TorrentManager.Dispose();
}
catch (Exception ex) {
//handle exception here, shouldnt really happen
}
finally {
lock (ManagerUnloadedLock) {
Monitor.Pulse(ManagerUnloadedLock);
}
}
}
}

Also available in: Atom PDF