如何在UWP中的异步ProgressBar上修复“编组为不同的线程错误”

时间:2019-04-30 19:19:50

标签: c# asynchronous uwp progress-bar

我正在使用UWP应用程序,该应用程序从服务器异步下载图像,图像下载部分工作正常,并且能够显示下载图像组的缩略图。下载这些图像时,我的问题显示进度条。

我知道您需要使用Dispatcher以避免Thread Exception。并且我正在使用它,从异步线程显示缩略图没有问题。

这里是我的代码。从“ OnNavigatedTo”中,我称为“ RunTasksParallel()”

//启动进度栏

constructor(inj: Injector) {
this.router = inj.get(AuthService) }

我的回叫方法如下所示,它基本上是在调用另一种方法来填充缩略图。

public void RunTasksParallel()
        {
    ProgressbarStart(this.UserImageRepo != null ?     convert.ToInt32(this.UserImageRepo.Images):100);

   System.Threading.Tasks.Task.Run(() =>  this.UserImageRepo.GetUserImagesAsync(ImageReceived));
        }

我有两种方法来处理进度条活动。

 bool ImageReceived(WriteableBitmap image, bool IsCompleted)
        {           
            if (IsCompleted)
            {
                imgloadprogress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }

            PopulateThumbnails(image);
            return true;
        }



 private async void PopulateThumbnails(WriteableBitmap image)
        {
            if (!Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
            {
               await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    PopulateThumbnailImages(image);
                    return;
                    // Your UI update code goes here!
                });
            }
 ProgressBarProgress();
// Heres my code to populate the image list everytime the first group image goes different collection which has binding to datagrid. so I get all thumbnails.
// code to populate Image lists. 
}

还有

private async void ProgressbarStart(int Maximum)
        {
            if (!Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                 () =>
                 {
                     ProgressbarStart(Maximum);
                     return;
                     // Your UI update code goes here!
                 });
            }

            imgloadprogress.Visibility = Windows.UI.Xaml.Visibility.Visible;
            imgloadprogress.Maximum = Maximum;
            imgloadprogress.Minimum = 0;
            imgloadprogress.Value = 0;
          //  imgloadprogress.val
        }

当我运行该应用程序时,出现以下错误“该应用程序调用了为另一个线程封送的接口。(HRESULT的异常:0x8001010E(RPC_E_WRONG_THREAD))”

您已经注意到,我只想显示一个进度条,因为Application正在收集图像,并且那些组标题图像(在组中接收到的第一张图像)进入绑定到网格的列表。如果我仅通过开始和停止将进度条更改为IsIndeterminate。它确实有效。

1 个答案:

答案 0 :(得分:1)

  

该应用程序调用了为另一个线程封送的接口。 (来自HRESULT的异常:0x8001010E(RPC_E_WRONG_THREAD))“

问题是您在非主线程中更新或初始化了接口。我检查了ProgressBarProgressProgressbarStart方法,您无需检测主线程HasThreadAccess是否具有递归方法。您可以直接使用主线程。

private async void ProgressbarStart(int Maximum)
{
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () =>
    {
        imgloadprogress.Visibility = Windows.UI.Xaml.Visibility.Visible;
        imgloadprogress.Maximum = Maximum;
        imgloadprogress.Minimum = 0;
        imgloadprogress.Value = 0;
    });
}


private async void ProgressBarProgress()
{
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
   () =>
   {
       if (imgloadprogress.Value + 1 > imgloadprogress.Maximum)
           imgloadprogress.Maximum = imgloadprogress.Maximum + 10;
       imgloadprogress.Value += 1;
   });
}