Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « An interview with John Walker - Part 3 | Main | Mirroring AutoCAD entities using .NET »

    September 29, 2008

    Tired of not being able to NETUNLOAD from AutoCAD? "Edit and Continue" to the rescue!

    A question came in on a previous post:

    Hello, I write applications for Autocad Map and Civil3d platforms, mostly with ObjectARX. I would like to do more with .NET but so far the main reason preventing this is not having the NETUNLOAD command.. With arx I can just arxunload and arxload the application for modifications in a second. But with .NET I have to restart the heavy environment and do all kinds of lengthy initializations before being able to try even small changes in code, this can take a minute or more.. Maybe it is possible to create an utility, for development purposes, to unload a .net assembly from Autocad ?

    NETUNLOAD has been requested many times by developers, but unfortunately would require a significant change for the .NET API - one it's unclear there'd be significant benefit in making. The root of the situation is that .NET assemblies cannot be unloaded from an AppDomain.

    To implement a NETUNLOAD command, we would have to host each assembly in a separate AppDomain and then destroy the AppDomain to unload it. It's altogether possible to implement your own bootstrapper assembly that does just this: I'm going to give that a try, to see how it works, but in the meantime I thought I'd point out (or remind you of) a capability that to greatly reduces the need to continually unload modules from AutoCAD: Visual Studio's Edit and Continue.

    Edit and Continue has been around since VB6, although you may not have looked at it in recent versions of Visual Studio. I personally didn't start finding it usable again until Visual Studio 2005 (and its Express Editions). If you'd previously turned your back on it, I suggest taking another look.

    To enable Edit and Continue, start by editing the Debugging options (accessed via Tools -> Options in the Visual Studio editor):

    Debugging Options - Edit and Continue

    If you attempt to edit the code before the module has been loaded, you'll get this error:

    Edit and Continue - assembly not loaded

    And if you try to edit the code before a breakpoint has hit, you'll get this altogether more obscure error:

    Edit and Continue - breakpoint not hit

    This message wasn't very helpful - at least not to me. My code wasn't running, as such (although I agree that someone's code was :-) and the setting mentioned was set correctly. Anyway - breaking into your code allows you to then edit the code, which should be obvious by a message on the application status-bar that tells you when the "continue" piece is successful:

    Edit and Continue - code changes applied

    I'd be curious to hear about others opinions of Edit and Continue: does it meet your debugging needs, or do you still see NETUNLOAD as important functionality? (There are clearly still areas - unrelated to debugging - where it might be useful to unload .NET assemblies. To be clear, I'd be happy to receive input on those areas, also.)

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e2010534dfff07970c

    Listed below are links to weblogs that reference Tired of not being able to NETUNLOAD from AutoCAD? "Edit and Continue" to the rescue!:

    Comments

    I kind of thought VSTA would help the issue, since the ide would already be inside the acad environment. But I wondering now, would VSTA allow you to stop all code, edit the project, and run again?
    If so, that will be when the .net api catches on with more people.
    Also, I have had problems getting edit and continue to work, even with settings correct.
    I think it might be that it only allows changes to the current line (shown yellow) or something. Fenton ran into this at AU last year, curious is you have ever seen this.

    Not all rountine can Edit and Continue.

    I don't know anything about how VSTA handles that - I would need to check with someone in our Revit team. I have no reason to believe it wouldn't support Edit and Continue.

    I'll ask Fenton about the issue he hit - I haven't hit these problems myself, but I'm probably a much lighter user of our APIs than he is. I've certainly been able to edit more than just the current line, for instance.

    Any details on cases scenarios where it doesn't work would be helpful to share.

    Kean

    thx.
    Also, are there instructions on how to make a menu image dll with VS?
    I used to use vb6 and a util that made the rsc file. I can use Resource Hacker too, but it only allows adding images one by one, not a whole folder at once.
    I tried making a dll with VS, but get clouds for icons, there must be a step I'm skipping.
    I've done lots of these menu dll's so know to close acad, delete mnr file...and so on.
    The dll itself (from VS 2008) is not right, so far, thx

    Hi guys,

    Hmmm, I can't really remember the exact issue that James was talking about above. I think it was simply because the debugger hadn't "broken" at the point which I thought it had (the stress of live coding!). Now I know how to use Edit and Continue effectively (meaning follow Kean's post above and don't change too much code while debugging) I don't really have problems with it.

    Actually, the only time I know of that really doesn't allow Edit and Continue is when "Enable Mixed Mode Debugging" is on.

    I hope this helps.
    Cheers
    Fenton

    Hi James,

    Sorry - you'll have to ask ADN support (or the discussion groups) about that one.

    Regards,

    Kean

    Hi Kean,
    First, I was looking for a way to 'NETunload' because my program looks for a code in the registry to determine if it should continue.
    If that code is not present, or not decryptable by my function, then the installation is not licensed, and therefore I want to unload it. I've been forced to merely unload the menu, and attempt to shut down autocad (although I might want to do something to intentionally crash autocad at that point)

    Second, long, long ago in a cubicle far, far away...(just downstairs actually). I used to be able to 'edit and continue', but at some point in time for reasons I can't explain, it stopped working. Having seen your article I tried to go back and check my settings, but I still can't get it to work now.

    All it tells me is that 'Cannot Currently Modify the text of the editor. It is read only' on the status bar.

    Worse than that, sometimes my watches and/or quick-watches (by hovering over variables during a break) show 'Nothing' or the initialized state of a variable, like a null string for a string, or zero for an integer, even when I know the variable has some other value stored in it. This problem makes it particularly difficult to find a problem in an algorithm when I can't examine the gozinas and gozoutas.

    One option would be to perform a check at the beginning of each command, and only execute the guts if the application is properly licensed.

    I'm not sure what the issue you're facing is due to: there may be a setting that needs to be made, although the watch variable issue seems very strange to me. Not something I've seen myself... I'm mostly using C#, which may make a difference.

    Kean

    Hi again..
    The watch problem is truly annoying, but my best guess is that it is an issue with VS 2005. I have just learned to be wary of watch values, and if needed I put debug messaging into the code to output the variables I want to watch. A little tedious, but it gets the job done.

    Now, I have searched all over the object browser, searched TTIF, searched the objectArx documentation, and I even tried (reluctantly) the activeX acFocusCtrl.dll that was for VBA, but I can not seem to 'activate' or 'focus' on the autocad window after I show a modeless form. I even found and tried to implement the Protected Overridable ReadOnly Property ShowWithoutActivation on the form, but it didn't change the behavior. I also tried converting my managed Document variable to the COM AcadDocument, which has an Activate method, but that did not work either. I would like to show the modeless form, which is for display purposes only, then return the focus to the Autocad window so the user can make selections. Any suggestions?

    I have used the edit & continue effectively, but I found another way around rebooting cad. I add a number to the end of my project name and increment that number as I make changes, and I keep loading the next dll. I'll do like 10 or 20 revisions then I'll start over and reboot cad to unload all the previous dlls.

    Hi Kean,
    You said "we would have to host each assembly in a separate AppDomain and then destroy the AppDomain to unload it. It's altogether possible to implement your own bootstrapper assembly that does just this".
    Actually application from separate AppDomain can't resolve some types from assembly "msvcm80, Version=8.0.50727.3053, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which use OpenMP 2.0 and throw (in deep) OpenMPWithMultipleAppdomainsException. I can't fix it.

    I think reason is here:
    http://msdn.microsoft.com/en-us/library/fw509c3b.aspx
    "If you attempt to load an application compiled with /openmp and /clr into a non-default application domain, a TypeInitializationException exception will be thrown outside the debugger and a OpenMPWithMultipleAppdomainsException exception will be thrown in the debugger."
    Some thoughts?

    David -

    As I suggested previously, the best way for a modeless UI to interact with AutoCAD is via the command-line. While you may not find it pretty it is the way we recommend, and comes with a number of advantages.

    Quiz -

    Sorry - this is put of my area of expertise. I suggest submitting it to the ADN support team, if you're a member.

    Kean

    Hi Kean,
    Thank you for reply.
    It isn't request for support, it is explanation why AutoCAD plugins can't be placed in separate AppDomain.
    I think there exist many other reasons why NETUNLOAD impossible (threads, sync, cross-domain interaction, etc ...) :)

    Hi Quiz,

    I see - thanks for clarifying.

    The separate AppDomains approach still seems likely to work for applications not built using /clr or /OpenMP (which I would think covers the majority of VB.NET and C# applications, at least). I'm still playing with this, but it is increasingly clear to me that the original design decision to use a single AppDomain was made for a good reason.

    Regards,

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search