• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Member
    CADKarasu
    Posts: 8
    Registered: ‎10-15-2010

    Problem with Try block and Transaction

    123 Views, 1 Replies
    02-07-2012 04:13 PM

    I have a VB.NET program which is very similar to another which functions fine.  In the troubled program when I enable a Try Catch block right after starting a transaction it invariably throws a Unhandeled Access Violation right at the transaction phrase.  If I remove the Try/Catch and replace it with an OnError Resume Next it completes execution but does not do what it is supposed to (extract four drawing border attribute values)

    I have checked the sequence against the program which functions and other than the troubled program being in ACAD2012 and VS 2010 they are pretty much identical (Resources, database access etc... )

    Any help or suggestions appreciated.

     

    CODE FOLLOWS:

     

    Note: DB established as: Dim db As Autodesk.AutoCAD.DatabaseServices.Database = SelectDatabase(f, False)

    (and then through selectdatabase): db.ReadDwgFile(fullname,FileOpenMode.OpenForReadAndAllShare, True, Nothing)

     

    ' Used in LoadAttributesFromDrawingIntoXML

    Function GetAttributes(ByVal db AsDatabase, ByVal filepath AsString) AsCollection

         Dim collection AsNewCollection

         Dim flag AsBoolean = False'

         Dim tr AsTransaction= db.TransactionManager.StartTransaction

     

         Try                ' - I enable the try and I get an unhandeled access violation. . .

         Dim id AsObjectId, bt AsBlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

         ForEach btrID AsObjectIdInbt

                 Dim btr AsBlockTableRecord = tr.GetObject(btrID, OpenMode.ForRead)

                acadEd.WriteMessage(" Btr name: "& btr.Name.ToString)

         If btr.IsLayout Then

           Dim l AsLayout = tr.GetObject(btr.LayoutId, OpenMode.ForRead)

                  Dim LayoutArr AsLayoutInfo = NothingForEach id Inbtr

                  Dim entity AsEntity = TryCast(tr.GetObject(id, OpenMode.ForRead), Entity)

                  If (Not entity IsNothing) Then If TypeOf entity IsBlockReferenceThen

                         Dim br AsBlockReference = TryCast(entity, BlockReference)

                  Dim borderNames = (From all In GetXelementFromCollection("BORDERINFO").Elements

                                  Select all.<NAME>.Value.ToUpper).Distinct

                   If (From all In GetXelementFromCollection("BORDERINFO").Elements Where

                     all.<NAME>.Value.ToUpper = br.Name.ToUpper Select all).Count > 0 Then

                         Dim attributeCollection As _                   

                         Autodesk.AutoCAD.DatabaseServices.AttributeCollection=

                         br.AttributeCollection

                          Dim IsValid AsBoolean = False

    Dim UniqueBlockName AsString = ""Dim BlkArr AsNewBlockInfo(br.Name,

                         IsValid, UniqueBlockName, br.Handle.Value)

     

                         ForEach atID AsObjectIdInattributeCollection

                                      Dim atRef AsAttributeReference = atID.GetObject(OpenMode.ForRead)

                                      Dim atInfo AsNewAttributeInfo

                                  WithatInfo

                                      ._Tag = atRef.Tag

                                          ._Text = atRef.TextString

                                          ._ObjectID = atRef.ObjectId.OldIdPtr.ToInt64

                                          ._BlockName = br.Name

                                          ._HandleValue = atRef.ObjectId.Handle.Value

                                      EndWith

                                  BlkArr.Add(atInfo)

                               Next

    If LayoutArr IsNothingThen

    LayoutArr = NewLayoutInfo(l.LayoutName)

                                      LayoutArr.Add(BlkArr)

                         Else

                               LayoutArr.Add(BlkArr)

                               EndIf

                    EndIf

               EndIf

           EndIf

      Next               'id'--add layout collections

      If Not LayoutArr IsNothingThen collection.Add(LayoutArr)

    EndIf'islayout

    Next'btr

     

    Catch ex As System.Exception

    tr.Abort()

    System.Windows.Forms.

    MessageBox.Show("Error in (GetAttributes)") ' & vbCr & ex.Message, "Error occured")

    db.Dispose()

    Finally

    tr.Dispose()

    db.Dispose()

    EndTry

    Return collection

     

    EndFunction

     

    Please use plain text.
    *Expert Elite*
    Posts: 6,460
    Registered: ‎06-29-2007

    Re: Problem with Try block and Transaction

    02-12-2012 12:33 PM in reply to: CADKarasu

    Hi,

     

    what I see is you do the db.Dispose twice in case you fall into the catch-block.

    What I don't see:

    • what error (exact error message) do you get
    • at what line does your debugger interrupt because of exception (I don't understand what you mean by "throws a Unhandeled Access Violation right at the transaction phrase" as I don't know what is the line with the "transaction phrase")

     

    - alfred -

     

    PS: please use this button 

    to transmit code to the message-text, this will avoid the deleting of a lot of spaces that makes it impossible to try your code without doing a lot of corrections.

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.