Esempio n. 1
0
        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DataGridTemplateColumn column  = this.grdList.Columns[0] as DataGridTemplateColumn;
            FrameworkElement       element = column.GetCellContent(this.grdList.Items[this.grdList.SelectedIndex]);
            var    bar         = column.CellTemplate.FindName("progressCtl", element) as ProgressBar;
            var    btnCancel   = sender as Button;
            Button btnDownload = column.CellTemplate.FindName("btnDownload", element) as Button;
            var    lblCancel   = column.CellTemplate.FindName("lblCancel", element) as Label;

            var index = this.grdList.SelectedIndex;

            Task.Factory.StartNew(() =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    btnCancel.Visibility = Visibility.Hidden;
                    lblCancel.Visibility = Visibility.Visible;
                    bar.Visibility       = Visibility.Hidden;
                }));

                if (DicMap.Keys.Contains(index))
                {
                    Thread td = DicMap[index];
                    DicMap.Remove(index);
                    DicIsStop[index] = true;
                    Thread.Sleep(500);
                    td.Abort();
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    lblCancel.Visibility   = Visibility.Hidden;
                    btnDownload.Visibility = Visibility.Visible;
                    btnDownload.IsEnabled  = true;
                }));
            });
        }
Esempio n. 2
0
        private void BtnDownload_Click(object sender, RoutedEventArgs e)
        {
            DataGridTemplateColumn column = this.grdList.Columns[0] as DataGridTemplateColumn;
            //获取指定的行与列相交位置的单元格
            FrameworkElement element = column.GetCellContent(this.grdList.Items[this.grdList.SelectedIndex]);
            var    bar         = column.CellTemplate.FindName("progressCtl", element) as ProgressBar;
            var    btnShow     = column.CellTemplate.FindName("btnShow", element) as Button;
            var    btnDownload = sender as Button;
            Border b           = btnDownload.Template.FindName("ContentContainer", btnDownload) as Border;
            Button btnCancel   = column.CellTemplate.FindName("btnCancel", element) as Button;

            string filePath = btnShow.Tag != null?btnShow.Tag.ToString() : string.Empty;

            if (string.IsNullOrEmpty(filePath))
            {
                var m_Dialog = new System.Windows.Forms.FolderBrowserDialog();
                System.Windows.Forms.DialogResult result = m_Dialog.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
                filePath = m_Dialog.SelectedPath.Trim();
            }

            var index   = this.grdList.SelectedIndex;
            var rowData = AppList[index];

            (sender as Button).IsEnabled = false;

            string downFoldar = filePath;

            btnShow.Tag = filePath;
            if (!Directory.Exists(downFoldar))
            {
                Directory.CreateDirectory(downFoldar);
            }

            Thread td = new Thread(() =>
            {
                DownloadHelper downLoadHelper = null;
                try
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        btnCancel.Visibility   = Visibility.Visible;
                        btnDownload.Visibility = Visibility.Hidden;
                    }));

                    string serverFileUrl = rowData.DownUrl;
                    downLoadHelper       = new DownloadHelper(serverFileUrl, downFoldar);
                    downLoadHelper.GetTotalSize();
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        bar.Maximum = downLoadHelper.TotalSize;
                    }));

                    string downLoadStep = ConfigHelper.GetAppSettingValue("DownloadStep");

                    downLoadHelper.Step = !string.IsNullOrEmpty(downLoadStep) ? Convert.ToInt32(downLoadStep) : 102400;

                    while (!downLoadHelper.IsFinished && !DicIsStop[index])
                    {
                        downLoadHelper.Download();
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            bar.Visibility = Visibility.Visible;
                            bar.Value      = downLoadHelper.CurrentSize;
                        }));
                    }

                    if (downLoadHelper.IsFinished)
                    {
                        string localFileName = downLoadHelper.FilePath;
                        string localMd5      = CommonHelper.GetMD5HashFromFile(localFileName);

                        if (localMd5 == rowData.MD5)
                        {
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                this.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    btnDownload.Visibility = Visibility.Hidden;
                                    btnShow.Visibility     = Visibility.Visible;
                                    btnCancel.Visibility   = Visibility.Hidden;
                                }));
                            }));
                        }
                        else
                        {
                            File.Delete(localFileName);
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                MessageBoxEx.Show(this, string.Format(Message.DownFail, rowData.AppName), Message.Tips, MessageBoxExButtons.OK, MessageBoxExIcon.Error);

                                btnDownload.Visibility = Visibility.Visible;
                                b.Background           = new ImageBrush
                                {
                                    ImageSource = new BitmapImage(new Uri(PRO_ReceiptsInvMgr.Resources.Common.IcoDownRestart))
                                };
                                btnDownload.IsEnabled = true;
                                btnShow.Visibility    = Visibility.Hidden;
                                btnCancel.Visibility  = Visibility.Hidden;
                                bar.Visibility        = Visibility.Hidden;
                            }));
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (!(ex is System.Threading.ThreadAbortException))
                    {
                        string errMsg = PRO_ReceiptsInvMgr.Resources.Message.NetworkError;

                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MessageBoxEx.Show(this, errMsg, Message.Tips, MessageBoxExButtons.OK, MessageBoxExIcon.Error);

                            btnCancel.Visibility   = Visibility.Hidden;
                            bar.Visibility         = Visibility.Hidden;
                            btnDownload.Visibility = Visibility.Visible;
                            btnDownload.IsEnabled  = true;
                        }));
                        Logging.Log4NetHelper.Error(typeof(AppDownload), string.Format(PRO_ReceiptsInvMgr.Resources.Message.DownAppFail, rowData.AppName) + ex.Message + System.Environment.NewLine + ex.StackTrace);
                    }
                }
                finally
                {
                    if (DicMap.Keys.Contains(index))
                    {
                        DicMap.Remove(index);
                    }
                    if (downLoadHelper != null)
                    {
                        downLoadHelper.Dispose();
                    }
                }
            });

            td.IsBackground = true;
            td.Start();

            DicMap.Add(index, td);

            if (!DicIsStop.ContainsKey(index))
            {
                DicIsStop.Add(index, false);
            }
            else
            {
                DicIsStop[index] = false;
            }
        }