.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Start-up & initialize

11 REPLIES 11
Reply
Message 1 of 12
quigs
892 Views, 11 Replies

Start-up & initialize

Hi,

I am looking at options to disable all functions during the start-up process when I netload “my.dll”.  The only way I have found is by using the following code:

Dim BoolRes As Boolean

If BoolRes = False Then

      Dim line As Line

      line.ColorIndex = 1

End If

 

The action of trying to change a lines colour while it’s not open for write works perfectly.  As in any of my functions after this process don’t load.  Does anyone know of a better way than my above code?  Or even why my code is a bad idea because it feels like a bad idea to me.

My name is Martin.. 😄
11 REPLIES 11
Message 2 of 12
norman.yuan
in reply to: quigs

I believe you are talking IExtensionApplicationo.Initialze() method that you implemeted in your "my.dll", so that when your DLL is laoded, the code inside Initialze(0 runs and after that your command defined with [CommandMethod] in your "my.dll" no longer works.

 

Probably you know the code you showed to run inside Initialize() is wrong and will crash (you maybe showed the wrong delibrately).

 

The thing is that Autodesk makes IExtensionApplication.Initialize() silently swallow exception that raised inside Initialize() and the result is the assembly loading would be aborted, thus useless.

 

So, one MUST be very carefully to handle possible exceptions that may be raised inside Initialize(). The common approach would be wrap all code in Initialze() with try...catch block:

 

public void Initialize()

{

   try

   {

     //Run your code here

   }

   catch

   {

     Document dwg=Application.DocumentManager.MdiActiveDocument;

     dwg.Editor.WriteMessage("\nSomething went wrong when initializing...)

   }

}

 

This way, even your code runs bad, the assemly loading will still be completed, and your custom commands in the assembly should be working.

 

However, you need to make sure how your commands should work, if your code in Initialize() failed. After all it serves some purpose to run code in Initialize(), which may impact how things can be done later.

Message 3 of 12
quigs
in reply to: norman.yuan

Yes in initialize that’s correct.

 

My software has 100's of commands all in separate .vb documents. Like so:

00.  StartUp_1.vb   = (Contains initialize)

01.  Function_1.vb = (Contains "function 1")

02.  Function_1.vb = (Contains "function 2")

03.  Function_1.vb = (Contains "function 3")

 

Each doc has a command, my initialize component checks my security on start-up.  I the security fails I want NO commands to load in all documents.  Like I said before, the sample section of code I shown at the top stops it dead in its tracks.  The code surrounded in a try catch would allow the rest of the commands to load wouldn't it?

My name is Martin.. 😄
Message 4 of 12
BlackBox_
in reply to: quigs

Methinks you're misunderstanding what Initialize() does... It gives you, the developer, an opportunity to prompt the user, configure some dependent settings, etc. when the code is loaded.... It does not prevent your subsequent classes from being loaded if some criteria is not met per-se.

 

Since there is no such pseudo-named NETUNLOAD functionality, you might consider using a two-stage assembly load procedure, such that if the necessary criteria is met via Initialize() when your primary assembly loads, your secondary assembly (the one with the CommandMethod attributes) is programmatically loaded... Else it is not loaded (i.e., no CommandMethods are registered).

 

Hope that makes (more?) sense.



"How we think determines what we do, and what we do determines what we get."

Message 5 of 12
quigs
in reply to: BlackBox_

Thanks peeps for the help, but one of my problems in life is not articulating myself.  I will try to simlify things.

 

How would I stop my dll loading from within my .dll using code when it's netloaded?  Like I said at the top, the code I have provided will do this, I'm just not sure how safe this is, or if there is some chance it may not stop it every time.

 

Ta,

 

Martin.

My name is Martin.. 😄
Message 6 of 12
BlackBox_
in reply to: quigs


@quigs wrote:

Thanks peeps for the help, but one of my problems in life is not articulating myself.  I will try to simlify things.

 

How would I stop my dll loading from within my .dll using code when it's netloaded?  Like I said at the top, the code I have provided will do this, I'm just not sure how safe this is, or if there is some chance it may not stop it every time.


You've articulated yourself just fine, IMO.

 

You've clearly explained that you intentionally evaluate a code snippet which presumably throws an eNotOpenForWrite Exception, that you subsequently neglect to Catch... Couldn't be more clear.

 

I've already offered one maintainable option, which precludes any Exception(s) from being raised... perhaps someone smarter than I will pass through offering another, perhaps simpler, option that I am unaware of.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 7 of 12


@BlackBox_ wrote:

@quigs wrote:

Thanks peeps for the help, but one of my problems in life is not articulating myself.  I will try to simlify things.

 

How would I stop my dll loading from within my .dll using code when it's netloaded?  Like I said at the top, the code I have provided will do this, I'm just not sure how safe this is, or if there is some chance it may not stop it every time.


You've articulated yourself just fine, IMO.

 

You've clearly explained that you intentionally evaluate a code snippet which presumably throws an eNotOpenForWrite Exception, that you subsequently neglect to Catch... Couldn't be more clear.

 

I've already offered one maintainable option, which precludes any Exception(s) from being raised... perhaps someone smarter than I will pass through offering another, perhaps simpler, option that I am unaware of.

 

Cheers


I don't think your suggestion would work with demand-loaded commands, and there's nothing wrong with throwing an exception. The only problem is that the OP seems to think that he must make an erroneous API call to raise an exception, rather than just throwing one.

Message 8 of 12
DiningPhilosopher
in reply to: quigs


@quigs wrote:

Hi,

I am looking at options to disable all functions during the start-up process when I netload “my.dll”.  The only way I have found is by using the following code:

Dim BoolRes As Boolean

If BoolRes = False Then

      Dim line As Line

      line.ColorIndex = 1

End If

 

The action of trying to change a lines colour while it’s not open for write works perfectly.  As in any of my functions after this process don’t load.  Does anyone know of a better way than my above code?  Or even why my code is a bad idea because it feels like a bad idea to me.


I would suggest becoming more familiar with the tools you're trying to use.

 

You raise exceptions using the Throw statement, not by making erroneous API calls.

Message 9 of 12
BlackBox_
in reply to: quigs

Fair enough; so you're sayin something like:

if (<criteria is not met>)
throw new <?.>Exception();

... Would do the trick?


"How we think determines what we do, and what we do determines what we get."

Message 10 of 12


@DiningPhilosopher wrote:

@quigs wrote:

Hi,

I am looking at options to disable all functions during the start-up process when I netload “my.dll”.  The only way I have found is by using the following code:

Dim BoolRes As Boolean

If BoolRes = False Then

      Dim line As Line

      line.ColorIndex = 1

End If

 

The action of trying to change a lines colour while it’s not open for write works perfectly.  As in any of my functions after this process don’t load.  Does anyone know of a better way than my above code?  Or even why my code is a bad idea because it feels like a bad idea to me.


I would suggest becoming more familiar with the tools you're trying to use.

 

You raise exceptions using the Throw statement, not by making erroneous API calls.


... Just saw this, which answers my recent question; for some reason this post didn't show up on my iPhone, but does on Computer.



"How we think determines what we do, and what we do determines what we get."

Message 11 of 12


@BlackBox_ wrote:
Fair enough; so you're sayin something like:

if (<criteria is not met>)
throw new <?.>Exception();

... Would do the trick?

Yes.  The managed runtime loader will catch any exception thrown.  If the OP wants to inform the user that the commands are not available, he can display a message before throwing the exception.

Message 12 of 12


@DiningPhilosopher wrote:

@BlackBox_ wrote:
Fair enough; so you're sayin something like:

if (<criteria is not met>)
throw new <?.>Exception();

... Would do the trick?

Yes.  The managed runtime loader will catch any exception thrown.  If the OP wants to inform the user that the commands are not available, he can display a message before throwing the exception.


Thank you for clarifying... Perhaps in error, I picked up that this was 'a bad thing' to do in Initialize, and didn't realize one could intentionally employ Exceptions to prevent other assembly code from being loaded.

 

In contrast, I did learn from Gile's LispException helper Classes that one could do so when evaluating a ResultBuffer; I clearly didn't connect the dots.

 

As always, I appreciate the knowledge you share with the community.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost