public async Task StartAsync() { var engine = new ClientEngine(); MonoTorrent.Torrent torrent = null; if (File.Exists(TorrentFileName)) { torrent = await MonoTorrent.Torrent.LoadAsync(TorrentFileName); } else if (!Url.StartsWith("magnet")) { torrent = MonoTorrent.Torrent.Load(new Uri(Url), Path.Combine(torrentDir, Path.GetTempFileName() + ".torrent")); } manager = torrent is null ? await engine.AddStreamingAsync(MonoTorrent.MagnetLink.Parse(Url), DownloadFolder) : await engine.AddStreamingAsync(torrent, DownloadFolder); await manager.StartAsync(); if (!manager.HasMetadata) { await manager.WaitForMetadataAsync(); } }
private bool DownloadTorrent(string path) { try { ClientEngine engine = new ClientEngine(new EngineSettings()); MonoTorrent.Torrent torrent = MonoTorrent.Torrent.Load(path); TorrentManager torrentManager = new TorrentManager(torrent, SaveFolder, new TorrentSettings()); engine.Register(torrentManager); System.Threading.Tasks.Task task = engine.StartAllAsync(); task.Wait(); // Keep running while the torrent isn't stopped or paused. while (torrentManager.State != TorrentState.Stopped && torrentManager.State != TorrentState.Paused) { Thread.Sleep(1000); if (torrentManager.Progress == 100.0) { // If we want to stop a torrent, or the engine for whatever reason, we call engine.StopAll() //torrentManager.Stop(); engine.StopAll(); break; } } InfoFormat("The torrent {0} download succeeded.", path); return(true); } catch (ThreadAbortException) { throw; } catch (Exception e) { ErrorFormat("An error occured while downloading the torrent {0}: {1}", path, e.Message); return(false); } }