5

I'm having a problem when setting the IactivewViews extent. I have done it the same way on other places and it does work. In here, it just throws me a LoaderLockException.

        // OLD CODE
        private void airspaceElementsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Name.StartsWith("feature_"))
            {
                IFeatureWorkspace workspace = ((IDataset)_LoadedAirspace.UnderlyingObject.Table).Workspace as IFeatureWorkspace;
                ITable relatedTable = workspace.OpenTable(e.Node.Parent.Name);

                int oid = GetNodeObjectId(e.Node);
                IRow tempRow = relatedTable.GetRow(oid);

                IGeometry geom = null;

                if (tempRow is IFeature)
                {
                    IFeature feat = tempRow as IFeature;

                    geom = feat.ShapeCopy;
                    ISpatialReference srCopy = ((IClone)geom.SpatialReference).Clone() as ISpatialReference;

                    IEnvelope envelope = new EnvelopeClass();
                    envelope.SpatialReference = srCopy;
                    envelope.Union(geom.Envelope);
                    envelope.Expand(.25, .25, false);

                    // error here!
                    IActiveView view = ((IMxDocument)m_application.Document).ActiveView;
                    view.Extent = envelope;
                }
                else
                    return;

                Marshal.ReleaseComObject(relatedTable);
            }
            else
                return;
        }

By the way, this is inside a IDockableWindowDef. Any chance that THAT is ruining things for me?

edit following the suggestions of my fellow friends, I did on two separate statements. First I'm trying to get IMxDocument from m_application and then casting it to IActiveView.

It crashes when I'm trying to cast m_application.Document to IMxDocument (check new code below)

    private void airspaceElementsTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node.Name.StartsWith("feature_"))
        {
            IFeatureWorkspace workspace = ((IDataset)_LoadedAirspace.UnderlyingObject.Table).Workspace as IFeatureWorkspace;
            ITable relatedTable = workspace.OpenTable(e.Node.Parent.Name);

            int oid = GetNodeObjectId(e.Node);
            IRow tempRow = relatedTable.GetRow(oid);

            IGeometry geom = null;

            if (tempRow is IFeature)
            {
                IFeature feat = tempRow as IFeature;

                geom = feat.ShapeCopy;
                ISpatialReference srCopy = ((IClone)geom.SpatialReference).Clone() as ISpatialReference;

                IEnvelope envelope = new EnvelopeClass();
                envelope.SpatialReference = srCopy;
                envelope.Union(geom.Envelope);
                envelope.Expand(.25, .25, false);

                // crash here
                IMxDocument document = m_application.Document as IMxDocument;
                IActiveView view = document.ActiveView;
                view.Extent = envelope;
            }
            else
                return;

            Marshal.ReleaseComObject(relatedTable);
        }
        else
            return;
    }
4
  • what happens if you cast IMxDocument.FocusMap as IActiveView? Commented Mar 1, 2011 at 14:26
  • 2
    I would try breaking your statement into two statements to determine whether it is the cast to IMxDocument or accessing the ActiveView property that is throwing the exception. Commented Mar 1, 2011 at 16:04
  • I tried both suggestions and both failed :(. Commented Mar 1, 2011 at 17:19
  • I suspect the LoaderLock exception only happens when you are within a VS debugging session?
    – Petr Krebs
    Commented Mar 1, 2011 at 17:49

1 Answer 1

3

Since your code is executing in an event handler, it may be that you are making calls to ArcObjects on a separate thread. Take a look at ESRI's documentation for writing multithreaded ArcObjects code, specifically the section Calling ArcObjects from a thread other than the main thread.

3
  • This could be it. I've had other problems with that before. What should it do? Just place the entire method separetly and call it from within the event? Commented Mar 1, 2011 at 18:01
  • I ran a quick test (version 9.3.1) and verified that this is not the issue. Perhaps it would help to post the rest of the code for your class that implements IDockableWindowDef. Commented Mar 1, 2011 at 19:01
  • Unfortunately I can't post the rest of the code. What I can tell you is that I load all my TreeNode objects using a cursor, which later is released. My tag object (of each node is the OID) and each category holds a table name. Commented Mar 1, 2011 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.