调用线程不能访问此对象,因为不同的线程拥有它 [英] The calling thread cannot access this object because a different thread owns it

查看:458
本文介绍了调用线程不能访问此对象,因为不同的线程拥有它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我可以在以下code不能创造CroppedBitmap?我有一个例外:

  

调用线程不能访问此对象,因为不同的线程拥有它。

如果我改变code到

  CroppedBitmap CB =新CroppedBitmap(新WriteableBitmap的(BF),新Int32Rect(1,1,5,5));
 

除了没有了吗?为什么呢?

code 1,异常为 cb.Freeze()

 公共主窗口()
{
    的InitializeComponent();

    ThreadPool.QueueUserWorkItem((0)=>
        {
            //加载一个大的图像文件
            变种BF = BitmapFrame.Create(
                新的URI(D:\\ 1172735642.jpg),
                BitmapCreateOptions.None,
                BitmapCacheOption.None);
            bf.Freeze();
            Dispatcher.BeginInvoke(
                新的Action(()=>
                    {
                        CroppedBitmap CB =新CroppedBitmap(BF,新Int32Rect(1,1,5,5-));
                        cb.Freeze();
                        //设置图片的来源CB ....
                    }),
                    DispatcherPriority.ApplicationIdle);
         }
    );
}
 

code 2,作品:

  ThreadPool.QueueUserWorkItem((0)=>
    {
        变种BF = BitmapFrame.Create(
                新的URI(D:\\ 1172740755.jpg),
                BitmapCreateOptions.None,
                //BitmapCreateOptions.DelayCreation,
                BitmapCacheOption.None);
        bf.Freeze();
        变种WB =新的WriteableBitmap的(BF);
        wb.Freeze();
        this.Dispatcher.Invoke(
            新的Action(()=>
            {
                变种R =新Int32Rect(1,1,5,5);
                CroppedBitmap CB =新CroppedBitmap(WB,R);
                cb.Freeze();
                //设置图片的来源CB ....
                Image.Source = CB;
            }),
            DispatcherPriority.ApplicationIdle);
    }
);
 

code 3,工程没有WritableBitmap:

  ThreadPool.QueueUserWorkItem((0)=>
    {
        变种BF = BitmapFrame.Create(
                新的URI(D:\\ 1172735642.jpg),
                BitmapCreateOptions.None,
                //BitmapCreateOptions.DelayCreation,
                BitmapCacheOption.None);
        bf.Freeze();
        VAR BF2 = BitmapFrame.Create(BF);
        bf2.Freeze();

        this.Dispatcher.Invoke(
            新的Action(()=>
            {
                变种R =新Int32Rect(1,1,5,5);
                的BitmapSource CB =新CroppedBitmap(BF2,R);
                cb.Freeze();
                //设置图片的来源CB ....
                Image.Source = CB;
            }),
            DispatcherPriority.ApplicationIdle);
    }
);
 

解决方案

您可以来看看这班反射。异常将上升cb.Freeze()。在

  CroppedBitmap CB =新CroppedBitmap(BF,新Int32Rect(1,1,5,5-));
 

情况的构造做了这样的事情:

  this.this.Source =来源;
 

所以源当前线程不被创建,所以异常会上升。 在

 新的WriteableBitmap的(BF)
 

情况下,构造与BF对象和新源在当前线程创建的同步,所以,没有例外将上升。 如果您有兴趣在深入细节,你可以随时反映基础库与反射:)

Why I can't create CroppedBitmap in the following code? I got an exception:

The calling thread cannot access this object because a different thread owns it.

If I change the code to

CroppedBitmap cb = new CroppedBitmap(new WriteableBitmap(bf), new Int32Rect(1, 1, 5, 5));

the exception is gone? why ?

Code 1, an exception at cb.Freeze():

public MainWindow()
{
    InitializeComponent();

    ThreadPool.QueueUserWorkItem((o) =>
        {
            //load a large image file
            var bf = BitmapFrame.Create(
                new Uri("D:\\1172735642.jpg"),
                BitmapCreateOptions.None,
                BitmapCacheOption.None);
            bf.Freeze();
            Dispatcher.BeginInvoke(
                new Action(() =>
                    {
                        CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));
                        cb.Freeze();
                        //set Image's source to cb....
                    }), 
                    DispatcherPriority.ApplicationIdle);
         }
    );
}

Code 2, works:

    ThreadPool.QueueUserWorkItem((o) =>
    {
        var bf = BitmapFrame.Create(
                new Uri("D:\\1172740755.jpg"),
                BitmapCreateOptions.None,
                //BitmapCreateOptions.DelayCreation,
                BitmapCacheOption.None);
        bf.Freeze();
        var wb = new WriteableBitmap(bf);
        wb.Freeze();
        this.Dispatcher.Invoke(
            new Action(() =>
            {
                var r = new Int32Rect(1, 1, 5, 5);
                CroppedBitmap cb = new CroppedBitmap(wb, r);
                cb.Freeze();
                //set Image's source to cb....
                Image.Source = cb;
            }),
            DispatcherPriority.ApplicationIdle);
    }
);

Code 3, works without WritableBitmap:

ThreadPool.QueueUserWorkItem((o) =>
    {
        var bf = BitmapFrame.Create(
                new Uri("D:\\1172735642.jpg"),
                BitmapCreateOptions.None,
                //BitmapCreateOptions.DelayCreation,
                BitmapCacheOption.None);
        bf.Freeze();
        var bf2 = BitmapFrame.Create(bf);
        bf2.Freeze();

        this.Dispatcher.Invoke(
            new Action(() =>
            {
                var r = new Int32Rect(1, 1, 5, 5);
                BitmapSource cb = new CroppedBitmap(bf2, r);
                cb.Freeze();
                //set Image's source to cb....
                Image.Source = cb;
            }),
            DispatcherPriority.ApplicationIdle);
    }
);

解决方案

You can look through this classes in reflector. Exception will rise in cb.Freeze(). In

CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));

case constructor did something like this:

this.this.Source = source;

So source wasn't created in current thread, and so exception will rise. In

new WriteableBitmap(bf)

case, constructor synchronize with bf object and new source is created in current thread, so, no exceptions will rise. If you are interested in In Depth details, you can always reflect base libraries with Reflector :)

这篇关于调用线程不能访问此对象,因为不同的线程拥有它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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