Solved

VB Timer Object

Posted on 2002-05-08
20
989 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:simonslr
  • 7
  • 6
  • 3
  • +4
20 Comments
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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.
0
 

Author Comment

by:simonslr
Comment Utility
No calls to DoEvents.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
are you sue thta problem is with timer?
Just a guess, maybe another part of the code is frezzing du to a special condition.
0
 

Author Comment

by:simonslr
Comment Utility
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).
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
What is your code in Form_Unload?

Note: This is just a comment and cannot be accepted as answer without my permission.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
simonslr,

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?

Anthony
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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...
0
 

Author Comment

by:simonslr
Comment Utility
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:

        SQLFreeStmt
        SQLDisconnect
        SQLFreeConnect
        SQLFreeEnv

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!!
0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
Comment Utility
>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?
0
 

Author Comment

by:simonslr
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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?
0
 

Author Comment

by:simonslr
Comment Utility
No - we never stop the timer.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
>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
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
>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.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
Make sure they have the latest SP for their NT machine.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
"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.
0
 

Author Comment

by:simonslr
Comment Utility
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.

 
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
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 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.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

7 Experts available now in Live!

Get 1:1 Help Now