?
Solved

ActiveX EXE with code in both Sub Main and Class_Initialize

Posted on 2002-07-30
3
Medium Priority
?
311 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:
Brendt Hess earned 150 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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