- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要使用来自第 3 方 API 的属性 getter,只是访问该 getter 有时 会挂起整个应用程序,有时 工作(在开发/调试器中) .如果我将它部署到生产服务器,主机应用程序有时会按预期工作,有时会因 KERNEL32 中的 APPCRASH 而死。
我不明白一个简单的 getter 怎么会卡住或崩溃任何东西,所以我认为这个属性可能是一种伪装的方法,并使用我最喜欢的反编译器来找出答案。
这是我遇到的方案/模式:
internal MyClass() : ISomeInterface
{
Locker = new object();
}
public object Locker { get; private set; }
private SomeObject _myProperty;
public SomeObject MyProperty
{
get
{
lock (Locker)
{
if (_myProperty== null)
MyProperty = SomeCondition ? SomeMethod() : Something.Else;
return _myProperty;
}
}
private set
{
_myProperty = value;
}
}
我相信每当 _myProperty
为 null
时,getter 调用 setter 并且这里有一个 race condition bug 是否正确?导致我遇到的随机/偶尔卡住?我如何确认/证实此卡住实际上是一个死锁?
什么是正确的实现?我被引导相信,如果 setter 也有一个 lock (Locker)
(我也不喜欢 Locker
可以公开访问,听起来像是自找麻烦) ,getter 将分配 _myProperty
而不是调用 setter,它是线程安全的。
最后,有没有一种方法可以在不需要第 3 方发布“固定”版本且不卡住的情况下调用此 getter?
我试过从主 UI 线程调用它,我试过从 Task
/后台线程调用它,我试过 Sleep(200)
调用,我尝试在 Task
上设置超时...无论我做什么,我似乎都无法始终如一地调用该 getter 并可靠地获得预期结果(它 确实工作......有时)。
Locker
对象是公共(public)的,但它是在一个内部类
中定义的,我只能通过它的接口(interface)访问它,当然不会公开它。有没有一种方法可以使用反射从外部获取锁 (Monitor.TryEnter(magicallyObtainedLocker)
) 并让它真正起作用?或者是 setter/getter 完全borked ?
最佳答案
Am I correct to believe that whenever _myProperty is null, the getter calls the setter and there's a race condition bug here that may be causing the random/occasional freezing I'm experiencing? How can I confirm/infirm that this freezing is effectively a deadlock?
没有。在 C# 中,lock
对于同一个线程是可重入的。您可以在 Monitor.Enter 的文档中看到这一点:
It is legal for the same thread to invoke Enter more than once without it blocking; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock.
don't like the Locker being publicly accessible either, sounds like asking for trouble
我同意你的看法。这更有可能是罪魁祸首。 Locker
是公共(public)的这一事实很可能意味着库中的其他东西正在锁定此对象,这可能是死锁的原因。
如果您可以通过 Visual Studio Concurrency Profiler 运行它,您应该能够在死锁处暂停,并及时看到对象在那个时间点阻塞。
关于c# - 有什么方法可以调用这个 getter 并始终如一地获得预期结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21489069/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!