久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

調用線程無法訪問此對象,因為不同的線程擁有

The calling thread cannot access this object because a different thread owns it(調用線程無法訪問此對象,因為不同的線程擁有它)
本文介紹了調用線程無法訪問此對象,因為不同的線程擁有它的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

為什么我不能在下面的代碼中創建 CroppedBitmap?我遇到了一個例外:

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.

如果我將代碼更改為

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

異常消失了嗎?為什么?

the exception is gone? why ?

代碼 1,cb.Freeze() 處的異常:

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);
         }
    );
}

代碼 2,有效:

    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);
    }
);

代碼 3,無需 WritableBitmap 即可工作:

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);
    }
);

推薦答案

您可以在反射器中查看這些類.cb.Freeze() 中會出現異常.在

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 構造函數做了這樣的事情:

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)

案例,構造函數與 bf 對象同步,并且在當前線程中創建了新源,因此不會出現異常.如果您對 In Depth 細節感興趣,您可以隨時使用 Reflector 反射基礎庫 :)

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 :)

這篇關于調用線程無法訪問此對象,因為不同的線程擁有它的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is there a way to know if someone has bookmarked your website?(有沒有辦法知道是否有人為您的網站添加了書簽?)
Use of Different .Net Languages?(使用不同的 .Net 語言?)
Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Determining an #39;active#39; user count of an ASP.NET site(確定 ASP.NET 站點的“活動用戶數)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Best way to keep track of current online users(跟蹤當前在線用戶的最佳方式)
主站蜘蛛池模板: 久久久综合久久 | 国产一区二区三区免费 | 国产精品视频免费观看 | 久久久91 | 99久久精品一区二区成人 | 99久视频| 羞羞视频在线免费 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 亚洲精品字幕 | 久久综合一区 | 成人av一区二区三区 | 国产精品揄拍一区二区 | 国产精品精品久久久 | 97精品国产97久久久久久免费 | 一区二区三区视频在线观看 | 亚洲视频精品在线 | 在线看黄免费 | 欧美一级免费看 | 成人免费一区二区 | 男人av在线| 成人在线电影在线观看 | 久久69精品久久久久久久电影好 | 色橹橹欧美在线观看视频高清 | 亚洲精品一区二区三区蜜桃久 | 久久蜜桃资源一区二区老牛 | 国产欧美精品一区 | 久久精品国产精品青草 | 国产综合久久久久久鬼色 | 久久久久久成人 | 午夜在线影院 | 亚洲视频免费在线观看 | 一区二区精品 | 青青激情网 | 国产精品欧美一区二区 | 免费v片 | 在线观看一区 | 久久久久国产一区二区三区四区 | 91精品国产综合久久久亚洲 | 久久久久国产 | 亚洲精品一二三区 | 国产欧美在线一区二区 |