ActiveX EXE with code in both Sub Main and Class_Initialize
Posted on 2002-07-30
My team has an ActiveX EXE with code in both Sub Main and Class_Initialize. The sticky part of the issue is that an object is instantiated in Sub Main and then referenced in Class_Initialize. The reason for this is that it takes from 30-45 seconds to get the EXE going (startup code) and we only want the user to incur that hit once per day (we keep the EXE resident in memory by using a hidden form). However, because of the code in Sub Main, it relinquishes its thread to the CPU (like a DoEvents would or launching a form). This allows Class_Initialize to begin to execute, where it gets an error code 91 because the object is not yet available. The issue is that when compiled (not in IDE mode) there are two threads of execution, one for Sub Main and one for Class_Initialize.
My question is (finally) - is it not a known VB "best practice" NOT to have code in Sub Main of an ActiveX EXE and if so, where can I find that documented for my skeptical teammates? This approach is clearly vulnerable (I proved it with DebugView) - I just need more voices that this design should be refactored away.
p.s. Sorry about the low point value but I just joined the forum and haven't accumulated any points yet.