代码之家  ›  专栏  ›  技术社区  ›  Jordan

如何防止不确定进度条冻结?

  •  2
  • Jordan  · 技术社区  · 14 年前

    一个不确定的进度条有什么好处,因为它当然是在同一个线程上而冻结的?有没有办法让它保持运转?可能是多线程?

    4 回复  |  直到 14 年前
        1
  •  1
  •   Jon Skeet    14 年前
        2
  •  1
  •   Jordan    14 年前

    protected void ShowProgress()
    {
        if (_progressThread != null)
            return;
    
            _progressThread = new Thread(() =>
            {
                var progressShell = Program.ViewFactory.CreateDialogShell();
                progressShell.DataContext = new ProgressViewModel(this);
                progressShell.Show();
    
                Dispatcher.Run();
            });
    
            _progressThread.SetApartmentState(ApartmentState.STA);
            _progressThread.Start();
    }
    
    protected void HideProgress()
    
    {
        if (_progressThread == null)
            return;
    
        _progressThread.Abort();
        _progressThread = null;
    }
    
        3
  •  0
  •   grantnz    14 年前

            ProgressIndicator.Visibility = System.Windows.Visibility.Visible;            
    
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    // Perform slow action
                    Thread.Sleep(5000);
    
                    this.Dispatcher.Invoke(
                        new Action(delegate()
                        {                                    
                                // Insert UI related activity here.
                                // ....
                                ProgressIndicator.Visibility = System.Windows.Visibility.Collapsed;                                                                     
                        }
                        ));
                }
            );
    
        4
  •  0
  •   FrustratedWithFormsDesigner    14 年前