无效的跨线程访问 [英] Invalid cross-thread access

查看:97
本文介绍了无效的跨线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个无效的跨线程访问时,我尝试在我的cam_CaptureImageAvailable方法使用的BitmapImage,我试图用一个调度员,但是这给了我一个无法访问已关闭的流错误,System.ObjectDisposedException是未处理的。

I am getting an Invalid cross-thread access when I try to use a BitMapImage in my cam_CaptureImageAvailable method, I tried using a Dispatcher but that gave me a Cannot access a closed Stream error, System.ObjectDisposedException was unhandled.

// Informs when full resolution picture has been taken, saves to local media library and isolated storage.
    void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
    {

        string fileName = folderName+"\\MyImage" + savedCounter + ".jpg";

        try
        {   // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = "Captured image available, saving picture ," + fileName;
            });

            // Save picture to the library camera roll.
            //library.SavePictureToCameraRoll(fileName, e.ImageStream);


            // Write message to the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                //txtDebug.Text = "Picture has been saved.";

            });

            // Set the position of the stream back to start
            e.ImageStream.Seek(0, SeekOrigin.Begin);


            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {

                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);

                    }

                }

                var bitmapImage = new BitmapImage(); 
                bitmapImage.SetSource(e.ImageStream); 
                WriteableBitmap wb = new WriteableBitmap(bitmapImage);
                IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
                using (IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName)) 
                { 
                    wb.SaveJpeg(myFileStream, 10, 10, 0, 70); 
                }

            }





        }
        finally
        {
            // Close image stream
            e.ImageStream.Close();
        }

    }



试过此以及

Tried this as well

Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    var bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(e.ImageStream);
                    WriteableBitmap wb = new WriteableBitmap(bitmapImage);
                    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
                    using (IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName))
                    {
                        wb.SaveJpeg(myFileStream, 10, 10, 0, 70);//compressing image
                    }
                });



谁能帮助我,我真的很感激它的感谢。

Can anyone help me I would really appreciate it thanks.

推荐答案

您必须创建的BitmapImage WriteableBitmap的在UI线程上,因为它们是的DependencyObject 秒。

You have to create the BitmapImage and the WriteableBitmap on the UI thread because they are DependencyObjects.

你的理由的ObjectDisposedException 的错误是,对于图像的流已经当调度员处理您的请求关闭。

The reason you get the ObjectDisposedException error is that the stream for the image is closed already when the dispatcher handles your request.

你有没有尝试代码移动到您的调度调用?

Did you try moving that code into your Dispatcher invocation?

 e.ImageStream.Close();

有关调度的代码不会立即得到,但在稍后的时间点执行的,但你必须已经关闭了流。

The code for the dispatcher does not get executed immediately but at a later point in time, but you have closed the stream already.

如果那不工作,那么你可以通过读取流内存在 CaptureImageAvailable ,然后通过临时解决你的问题该的MemoryStream 为源,以你的的BitmapImage

If that doesnt work, then you could workaround your problem by reading the stream to memory in CaptureImageAvailable and then pass that MemoryStream as source to your BitmapImage.

这篇关于无效的跨线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆