Solved

ActiveX EXE with code in both Sub Main and Class_Initialize

Posted on 2002-07-30
3
303 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

728 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