WP8:Dispatcher.BeginInvoke不对UI线程执行操作

时间:2014-02-21 15:28:31

标签: c# multithreading windows-phone-8 monogame begininvoke

我正在使用MonoGame将Windows Phone 7游戏移植到Windows Phone 8。我的游戏使用Texture2D.FromStream来加载来自互联网的图片,并且一旦它还没有在MonoGame中实现,我正在尝试建议的解决方法:在UI线程中使用BitmapImage加载图片。

以下是代码:

            Texture2D result = null;
            EventWaitHandle Wait = new AutoResetEvent(false);
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);
                WriteableBitmap writeImage = new WriteableBitmap(image);

                var index = 0;
                var pixels = new byte[writeImage.PixelWidth * writeImage.PixelHeight * 4];
                for (var h = 0; h < writeImage.PixelHeight; h++)
                    for (var w = 0; w < writeImage.PixelWidth; w++)
                    {
                        var pixel = writeImage.Pixels[index++];
                        var wIndex = w * 4 + h * writeImage.PixelWidth * 4;

                        pixels[wIndex++] = (byte)(pixel >> 16 & 0xFF);
                        pixels[wIndex++] = (byte)(pixel >> 8 & 0xFF);
                        pixels[wIndex++] = (byte)(pixel >> 0 & 0xFF);
                        pixels[wIndex++] = (byte)(pixel >> 24 & 0xFF);
                    }

                result = new Texture2D(graphicsDevice, writeImage.PixelWidth, writeImage.PixelHeight);
                result.SetData(pixels);
                Wait.Set();
            });

理论上一切都很好。但不知何故,动作代码永远不会被调用。 我在非ui工作线程中调用BeginInvoke,Deployment.Current.Dispatcher不为null并提供ui-thread调度程序。 游戏是由VS2012的MonoGame模板制作的WP8项目。 我试图在主线程(Game构造函数)中获取并保存为静态变量dispatcher和SynchronizationContext,以确保它们完全属于ui线程,但这没有用。

我也试过这样的代码:

SynchronizationContext uiThread = Hub.SyncContext;//set in main thread (Game constructor)
var waitHandle = new object();
lock (waitHandle)
{
    uiThread.Post(_ => 
    {
        lock (waitHandle)
        {

            BitmapImage image = new BitmapImage();
            image.SetSource(stream);
            WriteableBitmap writeImage = new WriteableBitmap(image);

            var index = 0;
            var pixels = new byte[writeImage.PixelWidth * writeImage.PixelHeight * 4];
            for (var h = 0; h < writeImage.PixelHeight; h++)
                for (var w = 0; w < writeImage.PixelWidth; w++)
                {
                    var pixel = writeImage.Pixels[index++];
                    var wIndex = w * 4 + h * writeImage.PixelWidth * 4;

                    pixels[wIndex++] = (byte)(pixel >> 16 & 0xFF);
                    pixels[wIndex++] = (byte)(pixel >> 8 & 0xFF);
                    pixels[wIndex++] = (byte)(pixel >> 0 & 0xFF);
                    pixels[wIndex++] = (byte)(pixel >> 24 & 0xFF);
                }

            result = new Texture2D(graphicsDevice, writeImage.PixelWidth, writeImage.PixelHeight);
            result.SetData(pixels);

           Monitor.Pulse(waitHandle);
        }

    }, null);

    Monitor.Wait(waitHandle);
}

但同样没有运气:执行在等待时停止,动作代码中的断点永远不会被触发。

更新:我刚刚添加了一个简单的代码,只有一个日志来示例MonoGame项目。 如果我等待AutoResetEvent,我从未看到任何日志。当我删除are.WaitOne()动作开始工作。我用are.WaitOne(1000)替换它并开始按预期工作(为什么?)。 但图像加载仍然没有运气。在我等待的时候看起来有些东西会阻止主线程运行.WaitOne()。

1 个答案:

答案 0 :(得分:3)

我使用这个库解决了问题: https://github.com/Taggersoft/ImageTools 这个fork完全适用于WP8,如果删除所有Contract.Requires之类的语句,它的效果非常好。 这是我的Texture2d.FromStream替换:

        public static Texture2D TextureFromStream(GraphicsDevice graphicsDevice, Stream stream, String dbg_nm)
    {           
        #if WP8
        byte [] b = new byte[4];
        stream.Read(b, 0, 4);
        stream.Seek(0, SeekOrigin.Begin);
        int stream_type = getStreamType(b, 0);
        Texture2D result = null;

        if (stream_type == 1 || stream_type == 0)
        {
            var image = new ExtendedImage();
            if (stream_type == 0)
                new JpegDecoderNano().Decode(image, stream);
            else
            {
                try
                {
                    new PngDecoder().Decode(image, stream);
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine("Error reading " + dbg_nm + ":" + ex.Message);
                }                   
            }

            var Colors = new Color[image.Pixels.Length / 4];
            for (var i = 0; i < image.Pixels.Length / 4; i++)
            {
                Color unpackedColor = new Color();
                unpackedColor.R = (byte)(image.Pixels[i * 4 + 0]);
                unpackedColor.G = (byte)(image.Pixels[i * 4 + 1]);
                unpackedColor.B = (byte)(image.Pixels[i * 4 + 2]);
                unpackedColor.A = (byte)(image.Pixels[i * 4 + 3]);
                Colors[i] = unpackedColor;
            }
            Texture2D texture2D = new Texture2D(graphicsDevice, image.PixelWidth, image.PixelHeight);
            texture2D.SetData(Colors);
            result = texture2D;
        }

        return result;

        #else
        return Texture2D.FromStream(graphicsDevice,  stream);
        #endif
    }

在游戏构造函数中:

    #if WP8
    Decoders.AddDecoder<PngDecoder>();
    Decoders.AddDecoder<JpegDecoder>();
    #endif

它的实际工作方式。但在我的情况下,这种方式比从uithread使用MS BitmapImage要好得多。