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

鎖定不會阻止 InvalidOperationException:對象當前正在

Locking does not prevent InvalidOperationException: Object is currently in use elsewhere(鎖定不會阻止 InvalidOperationException:對象當前正在其他地方使用)
本文介紹了鎖定不會阻止 InvalidOperationException:對象當前正在其他地方使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的印象是 lock() 會阻止多個線程同時訪問一個對象.

I was under the impression that lock() would prevent multiple threads from accessing an object simultaneously.

但是,以下代碼仍然經(jīng)常拋出 InvalidOperationException(對象當前正在其他地方使用):

But, an InvalidOperationException (Object is currently in use elsewhere) is still frequently thrown by the following code:

lock (this)
{
    localCopy = (Bitmap)this.bm.Clone();
    int x, y;
    float pX = this.p.x;
    int width = localCopy.Width;
    x = (int)Math.Round((double)(pX * (float)width));
    if (x >= localCopy.Width) x = localCopy.Width - 1;
    y = (int)Math.Round((double)(this.p.y * (float)localCopy.Height));
    if (y >= localCopy.Height) y = localCopy.Height - 1;
    colourPixel = localCopy.GetPixel(x, y);
}

注意事項:

  • 我拆分了x的計算來隔離異常的原因.它似乎來自訪問位圖.
  • 我嘗試創(chuàng)建位圖的本地副本,但這只會導致相同的異常.我試過 Clone()ing 并創(chuàng)建一個新的位圖.兩者都不起作用.
  • 我嘗試鎖定 this(如所見)和位圖對象.兩者都不起作用.
  • I split up the calculation of x to isolate the cause of the exception. It appears to come from accessing the bitmap.
  • I tried creating a local copy of the bitmap, but this just causes the same exception. I've tried Clone()ing and creating a new Bitmap. Neither works.
  • I've tried locking on this (as seen) and on the bitmap object. Neither works.

我是否試圖以一種不應該的方式使用 lock()?我誤解了它的目的嗎?如何防止 InvalidOperationExceptions?

Am I trying to use lock() in a way I'm not supposed to? Have I misunderstood it's purpose? How can I prevent InvalidOperationExceptions?

推薦答案

我最終找到了這個問題的根源.我把鎖移到了位圖屬性的getter/setter方法中,并實現(xiàn)了一個深拷貝"的方法來盡快釋放這個屬性.

I eventually got to the bottom of this. I moved the locks into the getter / setter methods of the bitmap property and implemented a "deep copy" method to free up the property as soon as possible.

private static object syncRoot = new Object();
private Bitmap _bm;
private Bitmap bm
{
    get
    {
        lock (syncRoot)
            return this._bm.DeepClone();
    }
    set
    {
        lock (syncRoot)
        {
            this._bm = value.DeepClone();
        }
    }
}

DeepClone() 擴展方法來自另一個問題的答案:

The DeepClone() extension method was cribbed from an answer to another question:

public static T DeepClone<T>(this T a)
{
    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, a);
        stream.Position = 0;
        return (T)formatter.Deserialize(stream);
    }
}

這篇關于鎖定不會阻止 InvalidOperationException:對象當前正在其他地方使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 色婷婷成人| 久久精品国产一区二区 | 男女啪啪免费视频 | 欧美一级久久 | 日韩福利在线 | 国产小视频在线播放 | 97人人看| a级片毛片| 色爽 | 久久久久久黄色 | 久草网站| 精品一二区 | 日韩一级视频 | 国产精品一区二区久久 | 久久久久一区 | 狠狠干婷婷 | 色婷av | 91久久国产综合久久 | 亚洲久久久 | 精品国产欧美 | 茄子视频色| 欧美操操操 | 黄色三级大片 | a在线播放 | 亚洲天堂男人 | 久久黄色大片 | www.中文字幕.com| 免费一级黄色录像 | 一区二区三区四区精品 | a在线免费观看 | 国产精品xxx在线观看 | 成人免费视频观看 | 黄色免费av | 日本特黄特色aaa大片免费 | 少妇久久久| 国产午夜精品久久 | 91精品国产成人www | 久久精品在线 | 免费视频a | 麻豆精品国产 | 精品视频一区二区三区 |