views:

416

answers:

4

A VB6 application is using the Interop Forms Toolkit to work with forms written in .NET. The documentation for the toolkit advises calling a method on the toolkit to advise the toolkit when the VB6 application is shutting down.

The VB6 application uses a Sub Main procedure that loads a splash screen, then displays several modeless forms. When Sub Main completes, the application is still running. How can the application detect that it is shutting down and call the cleanup method on the Toolkit?

A: 

Its been a while since I wrote in VB6 but if I remember correctly you can use the Unload event to call your cleanup code (it similar to the closing event in .net). You can also check that there are no other forms in the VB6 app still running

Leo Moore
The Unload event happens in the form. The app is showing several forms. I am looking for a central place to detect that the app is shutting down because there are over 400 forms in the app.
hurcane
+2  A: 

In a module (probably the same one that contains Sub Main), create a public sub (e.g AppCleanUp) that will hold your cleanup code.

Add a class to your project (e.g. clsAppCleanup). In this class, add code in the Class_Terminate event handler that calls the sub you created in the previous step.

In a module (probably the same one that contains Sub Main), define a variable of clsAppCleanup.

In Sub Main, instantiate the clsAppCleanup.

When the app is shutting down, The terminate event on the class will cause the cleanup code to run.

hurcane
A: 

There is a great screencast on this by Beth Massi:

http://msdn.microsoft.com/en-us/vbasic/bb643843.aspx

Mike L
This link only shows an app using a startup form, not a Sub Main. It does not help answer the question at all.
hurcane
A: 

Create a module that contains a FormCount variable. This variable will be shared by all forms in your application. Increment the FormCount variable in every form's Form_Initialize method. Decrement FormCount in every form's Form_Terminate method. When FormCount drops back to 0, you can notify your form toolkit that of the forms have been unloaded.

You won't have to worry about multi-threading issues because VB6 creates single-threaded applications, so one form's Initialize (or Terminate) method will run to completion before any others begin execution.

JeffK

related questions