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

如果鎖定的對象內部發生異常,它會保持鎖定狀

Does a locked object stay locked if an exception occurs inside it?(如果鎖定的對象內部發生異常,它會保持鎖定狀態嗎?)
本文介紹了如果鎖定的對象內部發生異常,它會保持鎖定狀態嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 c# 線程應用程序中,如果我要鎖定一個對象,假設是一個隊列,如果發生異常,該對象會保持鎖定狀態嗎?偽代碼如下:

In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here is the pseudo-code:

int ii;
lock(MyQueue)
{
   MyClass LclClass = (MyClass)MyQueue.Dequeue();
   try
   {
      ii = int.parse(LclClass.SomeString);
   }
   catch
   {
     MessageBox.Show("Error parsing string");
   }
}

據我了解,catch 之后的代碼不會執行 - 但我一直想知道鎖是否會被釋放.

As I understand it, code after the catch doesn't execute - but I have been wondering if the lock will be freed.

推薦答案

First;你考慮過 TryParse 嗎?

First; have you considered TryParse?

in li;
if(int.TryParse(LclClass.SomeString, out li)) {
    // li is now assigned
} else {
    // input string is dodgy
}

鎖會被釋放有兩個原因;首先,lock本質上是:

The lock will be released for 2 reasons; first, lock is essentially:

Monitor.Enter(lockObj);
try {
  // ...
} finally {
    Monitor.Exit(lockObj);
}

第二;您捕獲并且不重新拋出內部異常,因此 lock 實際上永遠不會看到異常.當然,您在 MessageBox 的持續時間內持有鎖,這可能是個問題.

Second; you catch and don't re-throw the inner exception, so the lock never actually sees an exception. Of course, you are holding the lock for the duration of a MessageBox, which might be a problem.

因此它將在除最致命的災難性不可恢復異常之外的所有異常中釋放.

So it will be released in all but the most fatal catastrophic unrecoverable exceptions.

這篇關于如果鎖定的對象內部發生異常,它會保持鎖定狀態嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(跟蹤當前在線用戶的最佳方式)
主站蜘蛛池模板: 2019精品手机国产品在线 | 久色激情| www.蜜桃av | 久久一区二区三区电影 | 日韩精品一二三区 | 中文字幕在线一区二区三区 | 国产一级淫片免费视频 | 国产乱码精品一品二品 | 国产一区二区三区视频 | 一区二区不卡高清 | 亚洲激精日韩激精欧美精品 | 中文字幕精品一区二区三区精品 | 久久久精品视频免费看 | av在线一区二区 | 在线看片国产精品 | 北条麻妃av一区二区三区 | 一级片在线观看 | 日韩视频二区 | 日韩一区二区三区在线观看 | 欧美在线资源 | 色爱综合 | 99reav| 69电影网 | 精品美女视频在线观看免费软件 | 欧美一级欧美三级在线观看 | 在线一区二区三区 | 日韩av美女电影 | 黄色国产视频 | 中文字幕第三页 | 玖玖国产| 伊人看片 | 97久久久久久久久 | 欧洲成人 | 一色桃子av一区二区 | 久久久久久久电影 | 欧美激情久久久久久 | 国产一区二区三区日韩 | 国产在线一区二区三区 | 天天色图 | 日韩视频精品在线 | 日韩欧美亚洲 |