Solved

ActiveX EXE with code in both Sub Main and Class_Initialize

Posted on 2002-07-30
3
295 Views
Last Modified: 2010-04-24
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.

Thanks,
R

p.s.  Sorry about the low point value but I just joined the forum and haven't accumulated any points yet.
0
Comment
Question by:RLCornish
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
ID: 7188757
See this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconsharingcoffeemonitor.asp

"Tip   The Initialize event of the first object a component creates is a good place to initialize the component. You’ll be much less likely to encounter object creation time-out problems and deadlocks that may occur if Sub Main is used to initialize the component. "

However, there are a number of people out there using ActiveX EXEs with sub Main implemented.  An ActiveX EXE is by definition asynchronous.  You have to add code (flags) to control timing if necessary.

One possibility would be something like this:

Public m_Started As Boolean

Sub Main()

... your code here ...

  m_Started = True
End Sub

In the Class_Initialize event:

Do While m_Started = False
  DoEvents
Loop

... your code here
0
 

Author Comment

by:RLCornish
ID: 7188893
Thanks bhess1 for your link.  It does answer my question.  I do however, challenge the sample code you posted.  My understanding is that Sub Main and Class_Initialize are running on two separate threads so Class_Initialize does not have access to the m_Started variable until AFTER Sub Main completes.  I did this test earlier and the flag was always False.

This is related to what M Curland said in his VBPJ article (June 1999, Create Efficient Multithreaded Apps")

Thanks.  The link is what I needed.  
R

p.s.  to others - more links are welcome.
0
 

Author Comment

by:RLCornish
ID: 7188900
Thanks bhess1.  I posted some comments in a reply because I forgot about this comment box.  Basically, I think the sample code you entered will not satisfy as a semaphore.  I tried this and always got a False when accessing the variable (m_Start) in Class_Initialize.  I think this is because they are executing on separate threads (Matt Curland, VBPJ, June 1999).

Thanks,
R
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question