private void PauseTorrent(object arg)
 {
     if (SelectedWrapper?.Manager.State == TorrentState.Downloading)
     {
         SelectedWrapper.PauseTorrent();
     }
 }
 private void StartTorrent(object arg)
 {
     if (SelectedWrapper != null)
     {
         if (SelectedWrapper.Manager.State == TorrentState.Stopped)
         {
             StartDownload();
         }
         else if (SelectedWrapper.Manager.State == TorrentState.Paused)
         {
             SelectedWrapper.ResumeTorrent();
         }
     }
 }
        private async void StartDownload()
        {
            var barProgress           = new Progress <double>(value => SelectedWrapper.Progress = value);
            var stateProgress         = new Progress <TorrentState>(value => SelectedWrapper.State = value);
            var stringProgress        = new Progress <string>(value => SelectedWrapper.ProgressString = value);
            var downloadSpeedProgress = new Progress <string>(value => SelectedWrapper.DownloadSpeed = value);

            var resultProgress = await Task.Factory.StartNew(
                () => SelectedWrapper.StartLoadAndProgressBarReporter(barProgress),
                TaskCreationOptions.LongRunning);

            SelectedWrapper.Progress = resultProgress;

            var resultState = await Task.Factory.StartNew(
                () => SelectedWrapper.TorrentStateReporter(stateProgress),
                TaskCreationOptions.LongRunning);

            SelectedWrapper.State = resultState;

            var resultProgressString = await Task.Factory.StartNew(
                () => SelectedWrapper.ProgressStringReporter(stringProgress),
                TaskCreationOptions.LongRunning);

            SelectedWrapper.ProgressString = resultProgressString;

            var resultDownloadSpeed = await Task.Factory.StartNew(
                () => SelectedWrapper.DownloadSpeedReporter(downloadSpeedProgress),
                TaskCreationOptions.LongRunning);

            SelectedWrapper.DownloadSpeed = resultDownloadSpeed;

            SelectedWrapper.Manager.TorrentStateChanged += async delegate(object o, TorrentStateChangedEventArgs args)
            {
                _engine.DiskManager.Flush(SelectedWrapper.Manager);
                var torrentHashString = SelectedWrapper.Torrent.InfoHash.ToString();
                var torrentInfo       = await _unitOfWork.GetRepository <TorrentInfo>().GetSingleOrDefaultAsync(torrentInfoPredicate =>
                                                                                                                torrentInfoPredicate.InfoHash == torrentHashString);

                torrentInfo.LastState = args.NewState.ToString();
                _unitOfWork.GetRepository <TorrentInfo>().Update(torrentInfo);
                await _unitOfWork.SaveChangesAsync();
            };
        }
        private void DeleteTorrent(object arg)
        {
            if (SelectedWrapper != null)
            {
                var deleteAction = new Action(async() =>
                {
                    _engine.Unregister(SelectedWrapper.Manager);
                    var torrentHashString = SelectedWrapper.Manager.InfoHash.ToString();
                    var searchResult      = await _unitOfWork.GetRepository <TorrentInfo>().GetSingleOrDefaultAsync(torrentInfo =>
                                                                                                                    torrentInfo.InfoHash == torrentHashString);
                    _unitOfWork.GetRepository <TorrentInfo>().Delete(searchResult);
                    await _unitOfWork.SaveChangesAsync();
                    SelectedWrapper.Manager.Dispose();
                    SelectedWrapper.DeleteFastResume();
                    File.Delete(SelectedWrapper.Torrent.TorrentPath);
                    TorrentsList.Remove(SelectedWrapper);
                });


                SelectedWrapper.PretendToDelete = true;

                if (SelectedWrapper.State == TorrentState.Stopped)
                {
                    _dispatcher.Invoke(deleteAction);
                }
                else
                {
                    StopCommand.Execute(null);
                    SelectedWrapper.Manager.TorrentStateChanged +=
                        delegate(object sender, TorrentStateChangedEventArgs args)
                    {
                        if (args.NewState == TorrentState.Stopped && SelectedWrapper.PretendToDelete)
                        {
                            _dispatcher.Invoke(deleteAction);
                        }
                    };
                }
            }
        }