/// <summary>
        /// responses to various button click scenarios
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //get the sender button
            Button btSender = (Button)sender;

            //for controller use content and others their name
            switch (btSender != btController ? btSender.Name : btSender.Content.ToString())
            {
            case "btTarget":

                new Thread((dwnlSource) =>
                {
                    try
                    {
                        //find the file name from the url and use it
                        SaveFileDialog selectTarget = new SaveFileDialog();
                        selectTarget.FileName       = Download.FindFileName((string)dwnlSource);
                        selectTarget.Title          = "Select a file to save download";
                        string dwnlTarget           = selectTarget.ShowDialog() == true ? selectTarget.FileName : "";

                        //set file path in application thread
                        Application.Current.Dispatcher.Invoke(() => txtTarget.Text = dwnlTarget);
                    }
                    catch (Exception ex)
                    {
                        //show exception message
                        MessageBox.Show(ex.Message, "Download Failed");
                    }
                }).Start(txtSource.Text);

                break;

            case "Start":

                //disable items on window to prevent inconsistency
                if (txtSource.Text.Length != 0 && txtTarget.Text.Length != 0)
                {
                    //disable UI
                    txtSource.IsEnabled
                              = txtTarget.IsEnabled
                              = btTarget.IsEnabled
                              = btSender.IsEnabled
                              = false;

                    //get the source and target url
                    string dwnlSource = txtSource.Text;
                    string dwnlTarget = txtTarget.Text;

                    new Thread(() =>
                    {
                        try
                        {
                            //create a new download job
                            download = new Download(dwnlSource, dwnlTarget);

                            //run the rest in application thread
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                //stop the download tracker and engine if they exist
                                if (downloadTracker != null)
                                {
                                    downloadTracker.Stop();
                                }
                                if (downloadEngine != null)
                                {
                                    downloadEngine.Abort().Join();
                                }

                                //create the tracker, engine
                                downloadTracker          = new DispatcherTimer();
                                downloadTracker.Interval = TimeSpan.FromSeconds(1);
                                downloadTracker.Tick    += Tracker;
                                downloadTracker.Start();

                                //create the engine
                                downloadEngine = new DownloadEngine(download, downloadTracker);
                                downloadEngine.Start();
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Download Failed");

                            //reset the UI in application thread
                            Application.Current.Dispatcher.Invoke(() => ResetUI());
                        }
                    }).Start();
                }
                else
                {
                    MessageBox.Show("Need Download source and target path", "Incomplete Data");
                }

                break;

            case "Pause":
                downloadEngine.Abort();

                break;

            case "Resume":
                downloadEngine.Start();

                break;
            }
        }
示例#2
0
 public void Add(Download download)
 {
     _dowloads.Add(download);
     download.PropertyChanged += PropertyChanged;
     download.PropertyChanged += Download_PropertyChanged;
 }
        /// <summary>
        /// download engine tracker updating the UI
        /// </summary>
        /// <param name="sender">dispatcher timer tracker</param>
        /// <param name="e">eventargs</param>
        private void Tracker(object sender, EventArgs e)
        {
            //tracking progress data
            string strSize      = Download.FormatBytes(download.DwnlSize);
            string strCompleted = Download.FormatBytes(download.DwnlSizeCompleted);
            string strSpeed     = "0.00 KBps";
            string strProgress  = txtProgress.Content.ToString();
            double valProgress  = 0;

            switch (downloadEngine.State)
            {
            case DwnlState.Create:
                strProgress = DotAnimation(strProgress, "Creating download job");

                break;

            case DwnlState.Idle:
                strProgress = "Waiting to start / resume download ● ● ●";

                //enable resume button
                btController.Content   = "Resume";
                btController.IsEnabled = true;

                break;

            case DwnlState.Start:
                strProgress = DotAnimation(strProgress, "Starting download");

                break;

            case DwnlState.Download:
                strSpeed    = Download.FormatBytes(download.DwnlSpeed) + "ps";
                valProgress = download.DwnlProgress;
                strProgress = DotAnimation(strProgress, string.Format("Downloading at {0:f2}%", download.DwnlProgress));

                //enable the pause button
                btController.Content   = "Pause";
                btController.IsEnabled = true;

                break;

            case DwnlState.Append:
                valProgress = download.AppendProgress;
                strProgress = DotAnimation(strProgress, string.Format("Appending at {0:f2}%", download.AppendProgress));

                //disable controller
                btController.IsEnabled = false;

                break;

            case DwnlState.Complete:
                valProgress = 100;
                strProgress = "Download complete ● ● ●";

                ResetUI();

                break;

            case DwnlState.Error:
                strProgress = "Download error ● ● ●";

                //display error message
                if (txtProgress.Content.ToString() != strProgress)
                {
                    MessageBox.Show(downloadEngine.Error.Message, "Download Failed");
                }

                ResetUI();

                break;

            case DwnlState.Abort:
                strProgress = DotAnimation(strProgress, "Aborting download");

                //disable controller button
                btController.IsEnabled = false;

                break;
            }

            //updatring progress data
            txtSize.Content     = strSize;
            txtComplete.Content = strCompleted;
            txtSpeed.Content    = strSpeed;
            txtProgress.Content = strProgress;
            barProgress.Value   = valProgress;
        }
示例#4
0
 public void SetCurrentDownload(Download download)
 {
     _selected = download;
     ModelStateChanged();
 }