Solved

VB Timer Object

Posted on 2002-05-08
20
993 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
ID: 6996640
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
ID: 6996647
No calls to DoEvents.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6996775
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:simonslr
ID: 6996795
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
ID: 6997685
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
ID: 6997720
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
ID: 6997740
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
ID: 6998252
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
ID: 6998623
>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
ID: 6998683
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6998703
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
ID: 6998711
No - we never stop the timer.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6998714
>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
ID: 6998737
>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
ID: 6998750
Make sure they have the latest SP for their NT machine.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6998823
"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
ID: 6998868
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
ID: 6998941
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
ID: 7722476
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
ID: 7780298
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 55
VBA to copy paste columns form one file to other 20 88
VBA: Select SQL query based on a config Sheet v2 11 44
How to Add / Edit Windows Menu 4 61
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

777 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