Book a Demo

Author Topic: C# getting a lock without crashing  (Read 4961 times)

Oliver Chappell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
C# getting a lock without crashing
« 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?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# getting a lock without crashing
« Reply #1 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
« Last Edit: December 16, 2010, 05:04:50 pm by Geert.Bellekens »

Oliver Chappell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: C# getting a lock without crashing
« Reply #2 on: December 16, 2010, 08:47:31 pm »
Thanks Geert
Thats great, I'll try it now.

Oliver Chappell

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: C# getting a lock without crashing
« Reply #3 on: December 16, 2010, 09:16:39 pm »
I can confirm this solution works, thanks again, we owe you one.