使用后台线程显示“忙碌指示器”

Moh*_*med 2 c# wpf multithreading

我正在主线程上进行一些繁重的计算,而这些计算无法在单独的线程上运行。

当这些计算正在运行时,我想在应用程序 UI 上显示一个“忙碌指示器”(即旋转小部件)。因此,我无法在主线程上显示繁忙指示器,因为在这些计算运行时 UI 被锁定。

为了解决这个问题,我尝试将忙碌指示器移动到单独的线程。在这篇文章的帮助下,我可以将忙碌指示器放在单独的线程上。但是,我无法与此线程通信以启动或停止繁忙指示器。

     private HostVisual CreateBusyIndicatorOnWorkerThread()

    {
        // Create the HostVisual that will "contain" the VisualTarget
        // on the worker thread.
        HostVisual hostVisual = new HostVisual();
        Thread thread = new Thread(new ParameterizedThreadStart(BusyIndicatorWorkerThread));
        thread.ApartmentState = ApartmentState.STA;
        thread.IsBackground = true;
        thread.Start(hostVisual);
        // Wait for the worker thread to spin up and create the VisualTarget.
        s_event.WaitOne();
        return hostVisual;
    }

 private static AutoResetEvent s_event = new AutoResetEvent(false);
 private void BusyIndicatorWorkerThread(object arg)

    {
        // Create the VisualTargetPresentationSource and then signal the
        // calling thread, so that it can continue without waiting for us.
        HostVisual hostVisual = (HostVisual)arg;
        VisualTargetPresentationSource visualTargetPS = new VisualTargetPresentationSource(hostVisual);
        s_event.Set();

        // Create a MediaElement and use it as the root visual for the
        // VisualTarget.
        visualTargetPS.RootVisual = CreateBusyIndicator();

        // Run a dispatcher for this worker thread.  This is the central
        // processing loop for WPF.
        System.Windows.Threading.Dispatcher.Run();
    }

  private FrameworkElement CreateBusyIndicator()

    {
        var busyIndicator = new MyBusyIndicator();
        //busyIndicator.DataContext = this.
        Binding myBinding = new Binding("IsBusy");
        myBinding.Source = this;
        busyIndicator.SetBinding(MyBusyIndicator.IsBusyProperty, myBinding);
     }
Run Code Online (Sandbox Code Playgroud)

我总是收到异常“调用线程无法访问此对象,因为不同的线程拥有它”。这是因为我试图从主线程更新繁忙指示器,而繁忙指示器由不同的线程拥有。

我也尝试了本文中给出的方法,

private void CreateAndShowContent()
    {
        Dispatcher = Dispatcher.CurrentDispatcher;
        VisualTargetPresentationSource source =
            new VisualTargetPresentationSource(_hostVisual);
        _sync.Set();
        source.RootVisual = _createContent();
        DesiredSize = source.DesiredSize;
        _invalidateMeasure();

        Dispatcher.Run();
        source.Dispose();
    }
Run Code Online (Sandbox Code Playgroud)

但是使用这种方法 Dispatcher.Run() 直到计算完成之后才会发生任何事情,然后显示忙碌指示器。

我想从主线程通信到有忙指示符的线程。有没有人有办法?

Ana*_*uza 5

没有理由在 UI 线程中运行“大量计算”。甚至更多 - 这是一种不好的做法。而是使用BackgroundWorkerwhich 可以工作,同时活动的 UI 线程将显示Loading/Calculating

var worker = new BackgroundWorker();

worker.DoWork += (s, e) => {
   // This part will last at a separate thread without blocking UI.
   // Excellent place for heavy computations.
}

worker.RunWorkerCompleted += (s, e) => {
   // Here we're back to UI thread - so you can change states and stop animations.
}

// And finally start async computation
worker.RunWorkerAsync();
Run Code Online (Sandbox Code Playgroud)

UI 应包含在您启动/完成工作时BusyIndicator将被激活/停止的控件。