Solved

ActiveX EXE with code in both Sub Main and Class_Initialize

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 58
Copy a row 12 57
VBA: Select SQL query based on a config Sheet v2 11 38
MS SQL store procedure to calculate and return result 6 49
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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now