C# 从后台线程获取结果并持续更新到UI线程UWP

C# 从后台线程获取结果并持续更新到UI线程UWP,c#,multithreading,uwp,C#,Multithreading,Uwp,我正在处理一个项目,该项目必须从服务器获取框架并将其显示到UI。所以我用这个方法不断地得到它。但是整个方法都是在UI线程上运行的,所以它稍微阻塞了我的UI。我们有没有办法让所有在不同线程上运行的方法都返回结果来更新UI线程下面的三行代码 注意:我试图用Dispatcher.RunAsync覆盖Task.Factory.StartNew的所有方法和下面三行,但没有成功。因为返回的值为null //width = 1280, height = 960 private async Task Disp

我正在处理一个项目,该项目必须从服务器获取框架并将其显示到UI。所以我用这个方法不断地得到它。但是整个方法都是在UI线程上运行的,所以它稍微阻塞了我的UI。我们有没有办法让所有在不同线程上运行的方法都返回结果来更新UI线程下面的三行代码

注意:我试图用Dispatcher.RunAsync覆盖Task.Factory.StartNew的所有方法和下面三行,但没有成功。因为返回的值为null

//width = 1280, height = 960 
private async Task DisplayPreviewTwocamera_Camera1(int width, int height)
{
    _isPreviewing = true;
    try
    {
        byte[] buffer;
        SoftwareBitmap softwareBitmap;
        SoftwareBitmapSource _imageSource;
        while (true)
        {
            if (_isMinimize)
            {
                return;
            }
            buffer = await StreamServiceHandler.Instance.GetStreamCamera_First_1();
            if (buffer.Length == 0)
            {
                buffer = null;
                continue;
            }
            softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer.AsBuffer(), BitmapPixelFormat.Gray8, width, height);
            buffer = null;
            if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
            {
                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            }
            //three lines of code below need to run in UI thread
            _imageSource = new SoftwareBitmapSource();
            await _imageSource.SetBitmapAsync(softwareBitmap);
            image_TwoCamera_Camera_1.Source = _imageSource;
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine(exception);
    }
}

您可以处理的方法是在单独的线程中运行非IU逻辑,然后调用UI线程对其进行更新:

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () => {
// UI THREAD
 });

你可以发布整个类,或者你调用DisplayPreviewTwocamera\u Camera1的pat吗?我不需要等待就从onNavigateTo方法调用它。我只是让它启动,然后忘记了是什么阻碍了线程,如果你分享这一部分,我可能会提供一个例子。