Exemplo n.º 1
0
        /// <summary>
        /// Starts a torrent.
        /// </summary>
        /// <param name="a_torrent">The torrent to start.</param>
        /// <remarks>
        /// Start()
        ///
        /// SYNOPSIS
        ///
        ///     Start(Torrent a_torrent);
        ///
        /// DESCRIPTION
        ///
        ///     This function will start the torrent by starting a task for
        ///     updating the tracker, processing incoming/outgoing blocks, updating
        ///     peers, and updating the GUI.
        ///
        /// </remarks>
        public void Start(Torrent a_torrent)
        {
            a_torrent.Status = "Started";

            // Task will update trackers.
            Task UpdateTracker = Task.Run(() =>
            {
                while (a_torrent.Started)
                {
                    a_torrent.UpdateTrackers();
                    Thread.Sleep(60000);
                }
            });

            // Task will check if there are any blocks to process.
            Task ProcessBlocks = Task.Run(() =>
            {
                while (a_torrent.Started)
                {
                    a_torrent.ProcessIncoming();
                    a_torrent.ProcessOutgoing();
                    Thread.Sleep(1000);
                }
            });

            // Will Update peers.
            Task UpdatePeers = Task.Run(() =>
            {
                while (a_torrent.Started)
                {
                    a_torrent.UpdatePeers();
                    Thread.Sleep(15000);
                }
            });

            // Will request blocks.
            Task RequestBlocks = Task.Run(() =>
            {
                while (a_torrent.Started)
                {
                    if (!a_torrent.Complete)
                    {
                        a_torrent.RequestBlocks();
                    }
                    Thread.Sleep(1000);
                }
            });

            // Updates the GUI for the current selected torrent.
            Task UpdateGUI = Task.Run(() =>
            {
                while (a_torrent.Started)
                {
                    if (m_selectedTorrent != null)
                    {
                        uiContext.Send(x => UpdateSelectedTorrentViews(m_selectedTorrent), null);
                    }
                    a_torrent.ComputeDownloadSpeed();
                    Thread.Sleep(2000);
                }
            });
        }