异步等待没有显示进度条

时间:2018-05-08 20:16:13

标签: c# wpf async-await

我有一些代码,我希望在单击按钮时显示不确定的进度条。同时,当显示进度条时,我正在启动任务并在该任务中执行耗时的操作并等待它。问题是单击按钮时进度条永远不会显示。请帮忙

private async void btnUpdateGrid_Click(object sender, RoutedEventArgs e)
        {
            stkProgressPanel.Visibility = Visibility.Visible;
            await Task.Factory.StartNew(() =>
            {
                UpdatePlate2DView();
            });
            stkProgressPanel.Visibility = Visibility.Collapsed;
        }

这是UpdatePlate2DView方法。我没有发布它的内部因为它有很多代码。此外,我正在运行调度程序,因为我遇到了一个跨线程异常。

private void UpdatePlate2DView()
        {
            try
            {
                //UIUtils.SetBusyState();

                var chartVM = ViewModel.GetActive2DVM();

                if (chartVM != null)
                {
                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        var ctrl = AllAnalysisControlsManager.GetAnalysisControl(chartVM);

                        if (ctrl != null)
                        {
                            m_2DChartCtrl = ctrl as Amplitude2DChartControl;

                            if (m_2DChartCtrl == null) return;

                            if (!m_2DChartCtrl.IsChartInitialized)
                            {//chart never shown to user. So, Show in hidden window so that UI is initialized.
                                var hiddenWindow = new Window //make sure the window is invisible
                                {
                                    Width = Settings.Default.Plate2DImageWidth,
                                    Height = Settings.Default.Plate2DImageHeight,
                                    WindowStyle = WindowStyle.None,
                                    ShowInTaskbar = false,
                                    ShowActivated = false,
                                    Content = ctrl,
                                    Opacity = 0.01,
                                    ResizeMode = ResizeMode.NoResize,
                                    AllowsTransparency = true

                                };

                                hiddenWindow.Show();
                            }

                            m_2DChartCtrl.UpdateUIEvenNotVisible = true;

                            ViewModel.SaveOldChartState();

                            string imageDir = ViewModel.GetImageDirectoryName();

                            ViewModel.DeleteExistingImages();

                            ViewModel.CreateDirectoryIfNotExists();

                            runData = ViewModel.Amplitude2DVM.AnalysisDataService.RunData;

                            for (int well = 0; well < runData.WellCount; well++)
                            {
                                ViewModel.SelectWell(well);

                                string wellName = WellNameUtils.GetWellName(well, runData.RowCount, runData.ColumnCount);

                                string filePath = System.IO.Path.Combine(imageDir, string.Format("{0}.png", wellName));

                                //System.Windows.Forms.Application.DoEvents();

                                m_2DChartCtrl.AnalysisChart.LightChart.SaveImage(filePath,
                                                                                 TargetImageFormat.Png,
                                                                                 Settings.Default.Plate2DImageWidth,
                                                                                 Settings.Default.Plate2DImageHeight, null,
                                                                                 false);

                                ViewModel.SetCellData(well, filePath);


                            }
                        }
                        ViewModel.RestoreOldChartState();
                        m_2DChartCtrl.UpdateUIEvenNotVisible = false;
                    }));

                }
            }
            catch (Exception ex)
            {
                SystemDebugLogLogger.LogError("UpdatePlate2DView:" + ex.Message);
            }

        } 

0 个答案:

没有答案