我有一个用于播放视频的 ActiveX COM 对象,它在 C# 应用程序中使用。它是这样声明的:
private AxVIDEOPLAYERUILib.AxVideoPlayerUI axVideoPlayerUI;
在我的代码中有这样的锁:
lock (axVideoPlayerUI)
{
axVideoPlayerUI.EnableControls = 1;
axVideoPlayerUI.Visible = true;
axVideoPlayerUI.ShowOverlay = 1;
axVideoPlayerUI.OverlayPosition = 3;
axVideoPlayerUI.Play();
}
但是我收到了我想删除的警告:
Warning 1 CA2002 : Microsoft.Reliability : 'VideoPlayerControl.LoadRecording(RecordVideo, int)' locks on a reference of type 'AxVideoPlayerUI'. Replace this with a lock against an object with strong-identity.
从这里链接http://msdn.microsoft.com/en-us/library/ms182290.aspx它声明以下对象具有弱身份:
MarshalByRefObject、ExecutionEngineException、OutOfMemoryException、StackOverflowException、字符串、MemberInfo、ParameterInfo、线程。
但我的对象不属于这些类别中的任何一个。
我也试过使我的对象静态,如下所述:C# lock and code analysis warning CA2002但这给了我错误:
Error 1 Member 'MyNameSpace.VideoPlayerControl.axVideoPlayerUI' cannot be accessed with an instance reference; qualify it with a type name instead
有谁知道我怎样才能摆脱原来的警告??
最佳答案
您可以只声明一个单独的对象用于锁定:
private AxVIDEOPLAYERUILib.AxVideoPlayerUI axVideoPlayerUI;
private object axVideoPlayerUILock = new object();
和:
lock (axVideoPlayerUILock)
{
...
COM 代理由 System.__ComObject
实现,后者又派生自 MarshalByRefObject
。也许指南会更清楚,派生的类型也不能使用。
关于c# - 我想删除警告 : "Replace this with a lock against an object with strong-identity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16981924/