Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Oliver Chappell on December 16, 2010, 05:06:18 am

Title: C# getting a lock without crashing
Post by: Oliver Chappell on December 16, 2010, 05:06:18 am
I'm trying to write code to get a lock.
Current component is an EA.Element
if (currentComponent.Locked)
{
        Utilities.writeToLog("Failed ");
 }
 else
{
      currentComponent.ApplyUserLock();
...

My problem is that the currentComponent.Locked always returns true, even where there is clearly not a lock on the component. Without this the software crashes if I try to get a lock on the component if someone else already has a lock.
Can anyone advise how to attempt to get a lock if you are not certain a component is not already locked without crashing please?
Title: Re: C# getting a lock without crashing
Post by: Geert Bellekens on December 16, 2010, 05:03:09 pm
This is how I do it:

Code: [Select]
/// locks the element by the current user
/// </summary>
/// <returns>true if successful</returns>
public override bool enableWrite()
{
      //because of some nasty bug in EA the application will crash if the element is already locked
      // by another user.
      // therefore we check first if this element is locked before even trying.
      string SQLQuery = "select s.entityID from t_seclocks s where s.entityID = '" + this.wrappedElement.ElementGUID + "'";
      XmlDocument result = ((EAModel)this.model).SQLQuery(SQLQuery);
      XmlNode lockNode = result.SelectSingleNode("//entityID");
      if (lockNode == null)
      {
            //no lock found, go ahead and try to lock the element
            try
            {
                  return this.wrappedElement.ApplyUserLock();
            }
            catch (Exception)
            {
                  return false;
            }
      }
      else  
      {
            //lock found, don't even try it.
            return false;
      }
}

Geert
Title: Re: C# getting a lock without crashing
Post by: Oliver Chappell on December 16, 2010, 08:47:31 pm
Thanks Geert
Thats great, I'll try it now.
Title: Re: C# getting a lock without crashing
Post by: Oliver Chappell on December 16, 2010, 09:16:39 pm
I can confirm this solution works, thanks again, we owe you one.