Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to remove warning: "Replace this with a lock against an object with strong-identity"

Tags:

c#

.net

com

I have an ActiveX COM object that is used to play video and its being used in a C# application. It is declared like so:

private AxVIDEOPLAYERUILib.AxVideoPlayerUI axVideoPlayerUI;

In my code there are locks on this like so:

lock (axVideoPlayerUI)
{
     axVideoPlayerUI.EnableControls = 1;
     axVideoPlayerUI.Visible = true;
     axVideoPlayerUI.ShowOverlay = 1;
     axVideoPlayerUI.OverlayPosition = 3;
     axVideoPlayerUI.Play();         
 }

But I get warnings that I want to get rid of:

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.

From this link here http://msdn.microsoft.com/en-us/library/ms182290.aspx it states that the following objects have a weak identity:

MarshalByRefObject, ExecutionEngineException, OutOfMemoryException, StackOverflowException, String, MemberInfo, ParameterInfo, Thread.

But my object doesnt fall into any of these categories.

I have also tried making my object static as described here: C# lock and code analysis warning CA2002 but that gives me errors:

Error 1 Member 'MyNameSpace.VideoPlayerControl.axVideoPlayerUI' cannot be accessed with an instance reference; qualify it with a type name instead

Does anyone know how I can get rid of the original warning??

like image 879
Harry Boy Avatar asked Dec 13 '25 05:12

Harry Boy


1 Answers

You can just declare a separate object to use for locking:

private AxVIDEOPLAYERUILib.AxVideoPlayerUI axVideoPlayerUI;
private object axVideoPlayerUILock = new object();

and:

lock (axVideoPlayerUILock)
{
   ...

COM Proxies are implemented by System.__ComObject, which in turn derives from MarshalByRefObject. Perhaps the guidance could be clearer that types derived from those mentioned are also not usable.

like image 135
Damien_The_Unbeliever Avatar answered Dec 15 '25 18:12

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!