Arnaud
Arnaud

Reputation: 467

Why is my DLL still being used by Revit after execution?

I have a small C# code I wrote for Revit API:

public List<string> Read_temp_file(string filename, Application app)
{
    string username = Environment.UserName;
    string myPath = "C:\\Users\\" + username + "\\AppData\\Roaming\\pyRevit\\" + app.VersionNumber + "\\";
    List<string> readlines = new List<string>();
    foreach (string file in System.IO.Directory.GetFiles(myPath, "*", SearchOption.AllDirectories))
    {
        if (file.Substring(file.Length - filename.Length,file.Length) == filename)
        {
            readlines = File.ReadLines(myPath + file).ToList();
        }
    }
    return readlines;
}

I build it as a DLL, then call it from my plugin in Revit, no problem. But then, if I come back to my code and want to re-build, I have an error message saying that the DLL is is being used by another process and blocked by Revit. Why?

Is there a special thing I need to do for Revit to "release" it after use? Or is it normal behavior?

Thanks a lot!

Upvotes: 0

Views: 1380

Answers (2)

DomCR
DomCR

Reputation: 583

Revit uses all the dlls, even when the command isn't executing, this is necessery to visualize the menus or to have the command accessible for the application.

If you want to debug your code, you can configure the project debug to target revit.exe and then pause the execution to change the code while you have revit open (I use Visual Studio 2017).

EDIT: (configure VS 2017 to debug in revit)

  1. Create a Class Library(.NET Framework) project in your solution.
  2. Go to project Properties -> Debug, select "Start external program" and find your revit.exe path. (C:\Program Files\Autodesk\Revit 2019\Revit.exe)
  3. To debug your current project you can setup the build to copy your dll directly to the folder that revit uses. Go to Build Events -> Post-build event command line, and write:

copy "$(ProjectDir)*.addin" "$(AppData)\Autodesk\REVIT\Addins\2019" copy "$(ProjectDir)bin\debug*.dll" "$(AppData)\Autodesk\REVIT\Addins\2019"

This commands will copy the adin and the dll to a folder that revit can find it, that way you will have your current build in revit.

4.(Extra step) you can also open a project automatically to make it easier, go to Debug-> Start options -> Command line arguments and write a path to a .rvt file.

Upvotes: 1

konrad
konrad

Reputation: 3716

Following up on the comment I made whether you are restarting Revit. I did a write up on my blog that explains how you can use the Revit Add-In Manager to achieve the result you are after:

http://archi-lab.net/debugging-revit-add-ins/

The difference between this, and a standard method of debugging is that Revit loads the DLL using the LoadFrom() method, locking it up for as long as the Revit.exe process is on, while the Add-In Manager uses the Load() method that only reads the byte[] of the DLL which means its available, and you can re-build your solution in VS, and reload in Revit without closing it. It does have drawbacks obviously so please read the post.

Upvotes: 1

Related Questions