private void Form1_Load(object sender, EventArgs e) { Serial s = new Serial(); basePath = Environment.CurrentDirectory; pluginPath = Path.Combine(basePath, "Plugins"); settingsFile = Path.Combine(basePath, "settings.data"); config = File.Exists(settingsFile) ? (Dictionary<string, string>)s.load(settingsFile) : new Dictionary<string, string>(); // init torrent lists localTorrents = new List<string>(); remoteTorrents = new List<string>(); loadPlugins(); // create background connection worker bgConnect = new BackgroundWorker(); bgConnect.DoWork += new DoWorkEventHandler(connect); bgConnect.RunWorkerCompleted += new RunWorkerCompletedEventHandler(connectComplete); bgConnect.WorkerSupportsCancellation = true; // create background update worker bgUpdate = new BackgroundWorker(); bgUpdate.DoWork += new DoWorkEventHandler(update); bgUpdate.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updateComplete); bgUpdate.ProgressChanged += new ProgressChangedEventHandler(updateProgress); // create torrent engine and load local files try { tEngine = new Transfer(basePath); tEngine.engine.TorrentRegistered += delegate(object o, MonoTorrent.Client.TorrentEventArgs ex) { localTorrents.Add(ex.TorrentManager.InfoHash.ToHex()); // Add known uTorrent client (seeder/peer) as we don't use the DHT network. if(config.ContainsKey("server")) ex.TorrentManager.AddPeers(new Peer("", new Uri("tcp://" + config["server"]))); }; tEngine.loadFastResume(); tEngine.engine.StartAll(); } catch (Exception ex) { Console.WriteLine(ex.Data); throw new Exception(); } // begin connection tryConnect(null); // prepare regular updater for ui. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; //timer.AutoReset = false; timer.Tick += delegate(object o, EventArgs ex) { timer.Stop(); // do list update draw(); // start timer again timer.Start(); }; // start regular ui updater timer.Start(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { Serial s = new Serial(); s.save(config,settingsFile); tEngine.stopDHT(); foreach (TorrentManager manager in tEngine.managerCollection) if (manager.State == TorrentState.Metadata) { manager.Dispose(); } tEngine.saveFastResume(tEngine.managerCollection); } catch (Exception ex) { MessageBox.Show(ex.StackTrace + "\n" + ex.Message); } }