VB Timer Object

We have a VB program that runs continuously. A Timer Control exists on the main form (which is not shown) which is set to fire every 9 seconds. On a particular site the program stops doing anything after a number of weeks and is swapped out of memory. We have put trace information into the application and it seems that the process is completing the code in its last call to the timer function but never starts a new one. i.e. it appears that the timer does not fire for some reason (or it does fire and the timer event is never picked up?).

Has anyone experienced this type of problem before or can anyone throw any light on possible causes?

P.S. This is related to my previous question on when Form_Load might get called (apart from at start-up) as the Form_Load function in our application does have a line of code to disable the timer under certain circumstances.
Who is Participating?
amebaConnect With a Mentor Commented:
>There is only one line of code in the Form_Unload - a call to a DLL function

So, no other cleanup code ... if form is later restarted, that can be a problem... I don't know your expertise, didn't see any of code, but, something is leaking memory - circular references, or, if you are using some object in DLLs, there must be some "Set obj = Nothing"

If some DLL is using ADO, it should keep the same connection, because freeing connection isn't fast and can cause leaks (when you set connection to nothing, it won't be destroyed immediately, but after some time, 10 minutes or so).

To prevent leaks, you should do all the cleanup manually, because VB will not do that very good.
Also, you should not declare object variables "As New" - such auto-instancing is hard to control...

I hope you checked if there is any unintentional call which causes Form_Load or if new Form is being loaded - by using forms.count or using Msgbox in Form_Load, or by checking for any appearance of "frmMain.someProperty" in your code.

You are not using some usercontrol on the form, right?
Do you have any DoEvents in the timer code?  If so, Microsoft recommends against this, as a DoEvents will surrender control of the processor until the Windows Event Queue is empty, which might be never if you have a busy machine.  In which case, your program would never regain control after the DoEvents.
simonslrAuthor Commented:
No calls to DoEvents.
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.

Richie_SimonettiIT OperationsCommented:
are you sue thta problem is with timer?
Just a guess, maybe another part of the code is frezzing du to a special condition.
simonslrAuthor Commented:
Not 100% sure, but as I say the trace information we output strongly indicates this as the last call to the timer function did complete successfully and there is no indication in the trace that the function has started again (we output trace as the first and last lines of the timer function).
What is your code in Form_Unload?

Note: This is just a comment and cannot be accepted as answer without my permission.
Anthony PerkinsCommented:

Welcome to EE, what ameba is referring to ("Note: This is just a comment and cannot be accepted as answer without my permission.") is typically here we give experts every chance to earn an "A" grade.  When even after doing all of this a lesser grade is merited than we give some explanation as to the reason. This is fully explained in the EE Guidelines (http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp) and I would recommend you re-read them.  Especially the section under:
How Do I Know What Grade to Give?

Thanks, Anthony, yes, the idea was *not* to interrupt collaboration process by "suddenly" closing the thread.
And to ask for clarification before giving B - I know TimCottee didn't/doesn't react on B, but if you asked, he would give excellent info, as always.

We need more info about the problem, OS, any dlls loaded/unloaded - do you load/unload form...
simonslrAuthor Commented:
OS: Windows NT4
Visual Basic 6

The Form_UnLoad function is not explicitly called by our software but would get called, for example, if the user logged off. In this case the program carries on and the timer does continue.

There is only one line of code in the Form_Unload - a call to a DLL function to close a connection to an Access database. This DLL is written in C++ and the function in question calls:


There are many DLLs used by the application - some written by us and some by third party software suppliers. None of the DLLs are explicitly loaded or unloaded.

For information, the Form_Load function checks the program arguments. If there are any arguments then the process is assumed to be running in automatic mode and the form is hidden and the timer enabled. If there are no arguments then the process runs in manual mode - the form is displayed and the timer is disabled. This is the only place in the code where the timer is disabled.

P.S. I will read the EE Guidelines!!
simonslrAuthor Commented:
I'm not sure how we have got onto the subject of memory leaks. The program does not leak memory.

We are using DAO for database access (I know its now better to use ADO!). The intention was to leave the database connection open. It had been felt that Form_Unload would only be called when the program terminated. We now know it gets called if a user logs off. But any way a new database connection is re-opened next time the database is accessed.

> I hope you checked if there is any unintentional call which causes Form_Load...
This more interesting. What sort of unintentional call can cause Form_Load to be called?  

I am thinking of removing the VB timer from the form and calling SetTimer (Windows API function) instead. The problem is that we cannot repeat the problem in our offices and I will need to wait several weeks before I know whether the problem has cleared on site.
Richie_SimonettiIT OperationsCommented:
SetTimer/KillTimer is a better option that timer control from VB due to acuracy.
besides, that leak of acuracy could be the problem.
I was thinking: when code in timer event is executed, you stop the timer, right?
simonslrAuthor Commented:
No - we never stop the timer.
>how we have got onto the subject of memory leaks

I have read this:
>On a particular site the program stops doing anything after a number of weeks and is swapped out of memory

and I think NT does not reclaim lost memory like Win2K does.

>What sort of unintentional call can cause Form_Load

As TimCottee mentioned in your previous question, something like this
   x = Form2.Timer1.Interval  ' this will load form2, if it isn't loaded

also, maybe you have this:
    Dim f As New Form2
    MsgBox f.Tag   ' this will load second instance of form2
>we cannot repeat the problem in our offices

If you also have NT, the problem can be - some other bad app is eating memory on their site.
Make sure they have the latest SP for their NT machine.
Richie_SimonettiIT OperationsCommented:
"No - we never stop the timer. "
So, in a lengthy process, this event could be called several times even if it hasn't finished previous one.
simonslrAuthor Commented:
The timer function only takes about 0.5 seconds. The timer is firing every 9 seconds ...

... but I am very interested by ameba's comments regarding Form_Load. As i have previously said, if the user logs off then Form_Unload is called. I have now found code in the application that references the form and, from what has been said, this will lead to form being reloaded. This is not good at all for our application which does all sorts of initialisation that should only happen once!! I am now busy on something else til tomorrow, but I will then verify or otherwise whether Form_Load is being called more than once. If it is then I need to re-structure the application. It shouldn't really be based on a form anyway when running in automatic mode.

I will let you know the results.

You'll need a form if you want to catch vbAppWindows (Current Windows session ending) or vbAppTaskManager (Windows Task Manager is closing the application) - to do a cleanup.
Hi simonslr,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept ameba's comment(s) as an answer.

simonslr, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
per recommendation

Community Support Moderator @Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.