Solved

ActiveX EXE with code in both Sub Main and Class_Initialize

Posted on 2002-07-30
3
272 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
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now