private void _alertEventHandler(object sender, DoWorkEventArgs e) { var timeout = TimeSpan.FromSeconds(0.5); var lastPost = DateTime.Now; var worker = sender as Worker.BackgroundWorker; while (!worker.CancellationPending) { if ((DateTime.Now - lastPost).TotalSeconds > 1) { _session.PostTorrentUpdates(); lastPost = DateTime.Now; } var foundAlerts = _session.Alerts.PeekWait(timeout); if (!foundAlerts) { continue; } var alerts = _session.Alerts.PopAll(); foreach (var alert in alerts) { Console.WriteLine(alert.GetType().ToString() + " :" + alert.Message); if (alert is TorrentAddedAlert) { OnTorrentAddedAlert?.Invoke(this, alert as TorrentAddedAlert); } else if (alert is StateUpdateAlert) { OnStateUpdateAlert?.Invoke(this, alert as StateUpdateAlert); } else if (alert is StateChangedAlert) { OnStateChangedAlert?.Invoke(this, alert as StateChangedAlert); } else if (alert is TorrentResumedAlert) { OnTorrentResumedAlert?.Invoke(this, alert as TorrentResumedAlert); } else if (alert is StatsAlert) { OnStatsAlert?.Invoke(this, alert as StatsAlert); } else if (alert is TorrentCheckedAlert) { OnTorrentCheckedAlert?.Invoke(this, alert as TorrentCheckedAlert); } else if (alert is SaveResumeDataAlert) { OnSaveResumeDataAlert?.Invoke(this, alert as SaveResumeDataAlert); } else { OnAlert?.Invoke(this, alert); } } } e.Cancel = true; }
private void monitor() { var timeout = TimeSpan.FromSeconds(0.5); int last_tick = Environment.TickCount; while (true) { if ((Environment.TickCount - last_tick) > 1000) { ses.PostTorrentUpdates(); last_tick = Environment.TickCount; } var foundAlerts = ses.Alerts.PeekWait(timeout); if (!foundAlerts) { if (running) { continue; } else { return; } } var alerts = ses.Alerts.PopAll(); foreach (var alert in alerts) { Type alert_type = alert.GetType(); if (alert_type == typeof(SaveResumeDataAlert)) { SaveResumeDataAlert srda = (SaveResumeDataAlert)alert; ResumeDataArrived(srda.Handle, srda.ResumeData); continue; } if (alert_type == typeof(TorrentAddedAlert)) { TorrentAddedAlert taa = (TorrentAddedAlert)alert; main_thread_dispatcher.Invoke(() => TorrentAdded(taa.Handle)); continue; } if (alert_type == typeof(StateChangedAlert)) { StateChangedAlert taa = (StateChangedAlert)alert; main_thread_dispatcher.Invoke(() => TorrentStateChanged(taa.Handle, taa.PreviousState, taa.State)); continue; } if (alert_type == typeof(StateUpdateAlert)) { StateUpdateAlert sua = (StateUpdateAlert)alert; foreach (var s in sua.Statuses) { main_thread_dispatcher.Invoke(() => TorrentStatsUpdated(s)); } continue; } if (alert_type == typeof(TorrentFinishedAlert)) { TorrentFinishedAlert tfa = (TorrentFinishedAlert)alert; main_thread_dispatcher.Invoke(() => TorrentFinished(tfa.Handle)); continue; } if (alert_type == typeof(MetadataReceivedAlert)) { MetadataReceivedAlert mra = (MetadataReceivedAlert)alert; MetadataReceived(mra.Handle); continue; } /* * case typeof(Ragnar.FileCompletedAlert): * case typeof(Ragnar.FileRenamedAlert): * * case typeof(Ragnar.PeerAlert): * case typeof(Ragnar.PeerBanAlert): * case typeof(Ragnar.PeerConnectAlert): * case typeof(Ragnar.PeerUnsnubbedAlert): * case typeof(Ragnar.PieceFinishedAlert): * * case typeof(Ragnar.ScrapeReplyAlert): * case typeof(Ragnar.StorageMovedAlert): * * case typeof(Ragnar.TorrentCheckedAlert): * case typeof(Ragnar.TorrentErrorAlert): * * case typeof(Ragnar.TorrentPausedAlert): * case typeof(Ragnar.TorrentRemovedAlert): * case typeof(Ragnar.TorrentResumedAlert): * * case typeof(Ragnar.UnwantedBlockAlert): */ System.Diagnostics.Debug.WriteLine(alert.Message); System.Threading.Thread.Sleep(150); } } }