Author Topic: Slow Method  (Read 8085 times)

0 Members and 1 Guest are viewing this topic.

HD

  • Guest
Slow Method
« on: August 25, 2008, 02:30:13 PM »
The code below populates a ComboBox control with drawing template (dwt) files. When I debug this code, the line:  AcadApplication acadApp = new AcadApplication(); can take up to 15 seconds before moving onto the next statement. Does anyone have any ideas why I am encountering such a delay?

Code: [Select]
        private void DetailSheetForm_Load(object sender, EventArgs e)
        {
            AcadApplication acadApp = new AcadApplication();
            string templatePath = acadApp.Preferences.Files.TemplateDwgPath;

            DirectoryInfo di = new DirectoryInfo(templatePath);
            FileInfo[] templateFiles = di.GetFiles("*.dwt");

            // Populate the Template ComboBox.
            foreach (FileInfo item in templateFiles)
            {
                if (!cboTemplate.Items.Contains(item))
                    cboTemplate.Items.Add(item);
            }
        }

p.s. My operating environment is: Vista Pro, Visual Studio 2008 Pro, and AutoCAD 2009.

Glenn R

  • Guest
Re: Slow Method
« Reply #1 on: August 25, 2008, 03:09:27 PM »
That's because this line:
Code: [Select]
AcadApplication acadApp = new AcadApplication();

is spining up a new instance of AutoCAD through it's COM API/Interface...I'm not surprised it's taking as long as you say.

Glenn R

  • Guest
Re: Slow Method
« Reply #2 on: August 25, 2008, 03:49:15 PM »
Not at all. You haven't supplied enough information for me to say 'live with it' just yet ;)

First off, is this a standalone executable externally automating AutoCAD, or is it a NETLOAD'ed .dll?

Glenn R

  • Guest
Re: Slow Method
« Reply #3 on: August 25, 2008, 03:56:39 PM »
Actually, what I should have said, was, is it your intent to spin up another AutoCAD session in that method?

Glenn R

  • Guest
Re: Slow Method
« Reply #4 on: August 25, 2008, 04:08:42 PM »
As far as the NETLOAD goes, that's what I suspected. Did you do a search here?

Glenn R

  • Guest
Re: Slow Method
« Reply #5 on: August 25, 2008, 04:40:59 PM »
Because I knew how to nab AutoCAD preferences, I didn't search here. ...

Well if that's the way you've been it doing from NETLOAD'ed .dll's, then it's...not optimal, shall we say :)

A quick search of this forum for 'Preferences.Files' (as that seems to be the root of what you're trying to get), revealed several hits, the 1st (at this point in time) being the most relevant (and it actually has the string 'TemplateDwgPath' in it too, as an added bonus ;) )

Here it is.

Glenn R

  • Guest
Re: Slow Method
« Reply #6 on: August 25, 2008, 04:41:57 PM »
Hope this helps, as firing up another ACAD session is not a good idea, especially from inside an existing one. If you have further questions, post away.

Glenn R

  • Guest
Re: Slow Method
« Reply #7 on: August 25, 2008, 05:03:54 PM »
No probs Nick - just 'steering' you along is all...

Glenn R

  • Guest
Re: Slow Method
« Reply #8 on: August 26, 2008, 04:31:10 AM »
Why are you doing this:

Code: [Select]
if (String.IsNullOrEmpty(templatePath) == true)

It's a bit verbose don't you think?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: Slow Method
« Reply #9 on: August 26, 2008, 06:05:40 AM »
You should probably remove this line as you are trying to release something you did not create

Code: [Select]
Marshal.ReleaseComObject(ap);

Glenn R

  • Guest
Re: Slow Method
« Reply #10 on: August 26, 2008, 06:31:43 AM »
Dan,

It decrements the COM Interface pointer reference count. As Nick is getting an interface pointer to AcadPreferences it seems valid.
However, the runtime will probably clean up regardless, so it's alright to remove it.

I've seen TonyT using this and I would think he would have a good reason to do so......

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: Slow Method
« Reply #11 on: August 26, 2008, 07:53:42 AM »
Tony T would be wrong in this case :)

Glenn R

  • Guest
Re: Slow Method
« Reply #12 on: August 26, 2008, 07:58:58 AM »
After more reading, I concur.

Glenn R

  • Guest
Re: Slow Method
« Reply #13 on: August 26, 2008, 09:50:21 AM »
The test is fine and is what I would do as well. What I meant is that this:

Code: [Select]
if (String.IsNullOrEmpty(templatePath) == true)

is returning a bool from evaluating a function, then testing for equality with == and finally the 'if' statemment does it's test.

I would have written it like so:

Code: [Select]
if (string.IsNullOrEmpty(templatePath)) return;

sinc

  • Guest
Re: Slow Method
« Reply #14 on: August 26, 2008, 10:16:24 AM »

It decrements the COM Interface pointer reference count. As Nick is getting an interface pointer to AcadPreferences it seems valid.

Blech!

Why is that method returning a COM object?  Just yet another way for Autodesk to complicate things and keep us on our toes?  Or is there some good reason for it?