ReportProgressChanged未更新UI

时间:2014-03-18 22:20:06

标签: c# wpf backgroundworker

我正在尝试使用多线程更新writeablebitmap,但是我没有设法让ProgressChanged事件触发任何对UI的更改。它虽然处于边缘,但一旦调用RunWorkerCompleted事件就会更新UI。我是否想念了ProgressChanged事件的目的或者是什么事?

progresschanged代码如下:

    private void BgWorkerMap_ProgressChanged_Handler(object sender, ProgressChangedEventArgs progressChangedArgs)
    {
        TestBitmap[7].Wb.AddDirtyRect(new Int32Rect(args[7].BgWorkerProperties.ProccessedX, args[7].BgWorkerProperties.ProccessedY, 256, 256));
        TestBitmap[7].Wb.Unlock();
        Src = TestBitmap[7].Wb;
        TestBitmap[7].Wb.Lock();
    }

这是我使用的完整代码。如果您尝试一下,您将在3秒后看到UI将被更新(添加一个线程睡眠1000毫秒/循环)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Testmultithredabitmap
{


public partial class MainWindow : INotifyPropertyChanged
{
    private readonly BackgroundWorker worker;
    private readonly Random random = new Random();
    private WriteableBitmap src;
    public WriteableBitmap Src
    {
        get { return src; }
        set
        {
            if (Equals(value, src)) return;
            src = value;
            NotifyPropertyChanged("Src");
        }
    }

    private class WorkerArgs
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public IntPtr BackBuffer { get; set; }
    }

    WorldMapFloor[] TestBitmap = new WorldMapFloor[14];

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Src = new WriteableBitmap(256, 256, 96, 96, PixelFormats.Bgr32, null);

        for (int i = 0; i < TestBitmap.Length; i++)
        {
            TestBitmap[i] = new WorldMapFloor(i);

            TestBitmap[i].Wb.Lock();
        }

        worker = new BackgroundWorker();

        worker.WorkerReportsProgress = true;

        worker.DoWork += WorkerOnDoWork;
        worker.ProgressChanged += BgWorkerMap_ProgressChanged_Handler;
        worker.RunWorkerCompleted += WorkerOnRunWorkerCompleted;
        worker.RunWorkerAsync(TestBitmap);

    }

    private void WorkerOnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs)
    {
        TestBitmap[7].Wb.AddDirtyRect(new Int32Rect(0, 0, 256 * 3, 256));
        TestBitmap[7].Wb.Unlock();
        Src = TestBitmap[7].Wb;
        //TestBitmap[7].Wb.Lock();
    }

    private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        WorldMapFloor[] args = (WorldMapFloor[])doWorkEventArgs.Argument;

        var width = args[7].BgWorkerProperties.Stride;

        int index = 0;

        while (true)
        {
            int startXCord = (index) * 256;
            int startYCord = (0) * 256;

            int filrStartIndexInBuffer = startXCord + startYCord * 256;

            args[7].BgWorkerProperties.ProccessedX = startXCord;
            args[7].BgWorkerProperties.ProccessedY = startYCord;

            Thread.Sleep(1000);

            unsafe
            {
                var buffer = (int*)args[7].BgWorkerProperties.pBackBuffer;
                for (var x = 0; x < 256; ++x)
                {
                    for (var y = 0; y < 256; ++y)
                    {
                        buffer[filrStartIndexInBuffer + x + y * (width / 4)] = random.Next();
                    }
                }
            }
            worker.ReportProgress(0, args);
            ++index;
            if (index == 3) break;
        }
    }

    private void BgWorkerMap_ProgressChanged_Handler(object sender, ProgressChangedEventArgs progressChangedArgs)
    {
        TestBitmap[7].Wb.AddDirtyRect(new Int32Rect(args[7].BgWorkerProperties.ProccessedX, args[7].BgWorkerProperties.ProccessedY, 256, 256));
        TestBitmap[7].Wb.Unlock();
        Src = TestBitmap[7].Wb;
        TestBitmap[7].Wb.Lock();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class BgWorkerMapProperties
    {
        public int ProccessedX;
        public int ProccessedY;

        public IntPtr pBackBuffer;
        public int Stride;


        public BgWorkerMapProperties(WriteableBitmap wb, int floorZ)
        {
            // Get a pointer to the back buffer. 
            pBackBuffer = wb.BackBuffer;
            Stride = wb.BackBufferStride;


        }
    }

    public class WorldMapFloor
    {
        public WriteableBitmap Wb = new WriteableBitmap(256 * 8, 256 * 8, 96, 96, PixelFormats.Bgr32, null);

        public BgWorkerMapProperties BgWorkerProperties;

        public string[] Files = null;

        public WorldMapFloor(int floorZ)
        {
            BgWorkerProperties = new BgWorkerMapProperties(Wb, floorZ);
        }

    }
}
}

感谢提前,来自绝望的WPF程序员。

2 个答案:

答案 0 :(得分:1)

通过Henk Holtermans评论我设法使其正确更新,整个原因似乎是你只会更新1个单位图,但因为我想将不同的位图存储为chache并快速交换它们。我所要做的就是将后备缓冲区数据复制到“主位图”

所以这就是progresschange事件的样子:

private void BgWorkerMap_ProgressChanged_Handler(object sender, ProgressChangedEventArgs progressChangedArgs)
{
        Src.Lock();

        int size = Src.BackBufferStride * Src.PixelHeight;

        // Copy the bitmap's data directly to the on-screen buffers
        // Method is DLL imported from kernel32.dll
        CopyMemory(Src.BackBuffer, TestBitmap[7].Wb.BackBuffer, size);

        Src.AddDirtyRect(new Int32Rect(0, 0, 256 * 3, 256));

        Src.Unlock();
}

在我的情况下,您甚至可以删除workercomplete事件,因为在UI上呈现位图数据后没有任何操作。

答案 1 :(得分:0)

对我来说,你似乎没有报告任何其他进度百分比,但是当工人完成它的工作时,它会报告100%。

你需要在你的流程中的不同点调用worker.ReportProgress(百分比)。 You can read more here