Internal Exception thrown on DocumentOpen

Internal Exception thrown on DocumentOpen

kraftwerk15
Advocate Advocate
368 Views
1 Reply
Message 1 of 2

Internal Exception thrown on DocumentOpen

kraftwerk15
Advocate
Advocate

I have an issue where I was attempting to call DocumentOpen from inside of a DoWork background task. I have included some snippets below.

public UpdateFamilyViewModel(int revitYear)
        {
            RevitYear = revitYear;
            CancelRequested = false;
            CanCancel = false;
            CanStart = true;
            CanFinish = true;
            DeleteExisting = false;
            DeleteBackups = false;
            StartTime = "Waiting for Process to Start";
            CompletedTime = "Waiting for Process to Start";
            ElapsedTime = "00:00:00";
            CompletionLabel = "Estimated Completion Time";
            _worker = new BackgroundWorker();
            _worker.DoWork += DoWork;
            _worker.WorkerReportsProgress = true;
            _worker.ProgressChanged += ProgressChanged;
        }

        private void Start()
        {
            _StartDateTime = DateTime.Now;
            StartTime = string.Concat(DateTime.Now.DayOfWeek, " ", DateTime.Now.Hour, ":", DateTime.Now.Minute, ":", DateTime.Now.Second);
            CompletionLabel = "Estimated Completion Time";
            CompletedTime = "Process starting";
            CanCancel = true;
            CanStart = false;
            CanFinish = false;
            _elapsedStopwatch.Restart();
            _worker.RunWorkerAsync();
        }

 

Inside of DoWork for the worker, I am calling for a set of documents to be opened. Like so:

 

private void DoWork(object sender, DoWorkEventArgs e)
        {
            SaveAsOptions saveAs = new SaveAsOptions()
                { Compact = true, OverwriteExistingFile = true };
            int thing = families.Count * 2;
            TotalFamilies = families.Count;
            FamiliesRemaining = families.Count;
            TimeSpan ts = new TimeSpan(0, thing, 0);
            _CompletedDateTime = _StartDateTime.Add(ts);
            CompletedTime = string.Concat(DateTime.Now.DayOfWeek, " ", _CompletedDateTime.Hour, ":", _CompletedDateTime.Minute, ":",
                _CompletedDateTime.Second);
            if (families.Count > 0)
            {
                for (int i = 0; i < families.Count; i++)
                {
                   //Omitting items here that do not pertain to the issue.
                    try
                    {
                        new OpenDocument(families[i].Path);
                        FamiliesCompleted++;
                    }
                    catch (Exception es)
                    {
                        Console.WriteLine(es);
                    }
                    ElapsedTime = _elapsedStopwatch.Elapsed.ToString();
                    FamiliesRemaining--;
                    if (CancelRequested)
                        break;
                    //Reports the progress
                    (sender as BackgroundWorker).ReportProgress(flot);
                }
            }
}

 

And finally when I open the document.

 

internal Document OpenDocument(string family)
        {
            System.Diagnostics.Debug.WriteLine(string.Concat("Beginning Opening the Document from: ", family.FamilyPath));
            if (_app is null) throw new NullReferenceException();
            Document doc = null;
            try
            {
                doc = _app.OpenDocumentFile(family.FamilyPath);
            }
            catch (Autodesk.Revit.Exceptions.InternalException ex)
            {
                System.Diagnostics.Debug.WriteLine("Internal Exception thrown on Document open.");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("General Exception on Document open.");
            }
            System.Diagnostics.Debug.WriteLine("Family Opened");
            return doc;
        }

 

Even though I am calling this window in a ShowDialog inside of an ExternalCommand, I believe the Background Thread is not allowed to open the Document. Confirming this with this post: https://thebuildingcoder.typepad.com/blog/2014/05/multithreading-throws-exceptions-in-revit-2015.htm...

 

You will notice in my OpenDocument method that I was wrapping the OpenDocumentFile from the API inside of a try catch that explicitly catches for an InternalException. I was noticing while running that the API was returning an InternalException instead of what I intended of a InvalidOperationException. Also, other Events inside of the Solution were firing because something behind the scenes were still allowing other events to fire even when there was not valid Document for those events to latch on to.

 

I am using KennanChan's Revit.Async in other portions of the Solution where I am running on a Background Thread in a Window.Show() context to update a UI instead of .ShowDialog() and all of my logic still exists in the IExternalCommand. I will most likely start converting this over to raising an event handler rather than allowing it to process in a traditional way because I was wanting a similar UI that is not blocked by the application.

 

Just leaving this here to document the InternalException error rather than an InvalidOperationException on the OpenDocumentFile. 

 

0 Likes
369 Views
1 Reply
Reply (1)
Message 2 of 2

Maltezc
Advocate
Advocate
Can you share an image of the error as well as the error message?