Breakpoint On the Timer.Enabled in Vb6

During my main processing I set enabled = true and expect the timer to fire.

The weird part is that if I set a breakpoint on my timer.enabled = true line and then allow the program to run on it all works fine. If I take the breakpoint off and run the program as normal then the timer never fires!!

Thanks. The timer interval is set to 5000 and even after counting slowly to 10 the event is not firing!

I can even prove it by setting another breakpoint on the first line of code in the timer firing event. When I set the original breakpoint and then let the program run on then the second breakpoint is reached. If I take out the first breakpoint and let the program just run the second breakpoint is never reached.

If you wish a timely correct response, you should always provide more info (and perhaps some code).
What is your Interval set to?
Where during your main processing do you set enabled - true?
What else is going on in your program at that time?
Did you try creating a tiny project, that mimics your problem?
Here is one that I prepared -
Option Explicit
'Settings done in the IDE -
' Enabled = False
' Interval = 5000

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Print Time
End Sub

That runs correctly, and starts 'printing' (cough!) the time, every 5 seconds, after I click the command button.

Try again. Please ignore my last post.
This is a snippet of code:
If mblnShown = True Or mblnPicCheck = True Then
Me.timTimerpicture.Enabled = True
Me.timTimerpicture.Interval = 5000
DoEvents
End If
Exit Sub
(This is the exit point for this sub)
and:-

If I put a breakpoint on the line:- me.timTimerpicture.Enabled = True (after the test for mblnshown) and on the line:- If mblnshown = False.....etc (in the timer code) and then, when the code breaks, click on the continue arrow then I get to the second breakpoint after 5 seconds, no problem.
If I now remove the first breakpoint on the timer.Enabled line and run the code I NEVER get to the second breakpoint.
Weird or what?
Roy

To use the timer within vb6 a lot to handle various refresh issues, you need to let the computer wait fresh the screen and then carry on.

This is usually caused by having lots of panels / pictures / etc all requiring a refresh beforehand and their not being enough time before the computer wants to start

Trick for you.

Set the panel enabled to false from the design mode, then when you hit your timer

If (x).enabled=false - wait again

Within the delay -

(x).refresh
Dummy=doevents
(x).enabled=true

So you are forcing the system to call the timer routine twice if the first time its not all ready

Does that make any sense ?

I have dozens of apps using vb6 with the original Sheridan 3d control - the newer operating systems such as windows7 update the screen in a different way to produce speedier results for the end user and hence has some refresh issues.

I don't cherish the thought of re-writing years of coding just for that !

PS Don't use break points when trying to work out what is going on, as the use of the break points makes results practically meaningless, or at least confusing.
Use Debug.Print's to work out what is going on, (where and when).

Terry,
I have the breakpoint on the line that says me.timTimerpicture.Enabled = True so it MUST be getting mblnShown = True or mblnPic = True. The second breakpoint is purely to see if the timer is firing - which without the first breakpoint being set it is not. The timer is set to false in the ide - it is never used until this point.

I think that perhaps Dave is getting the nearest. This has to be some sort of Windows refresh problem. The bit you are not seeing is that a new form showing a picture is launched just before this section. The timer is controlling the display of the picture. There might be 3 or 4 pictures and each time the timer fires the picture is rolled on to the next one in a rotation of pictures. So in principle the user sees picture 1 then 5 seconds later sees picture 2 and so on. At the end they go back to picture 1.

I have tried putting in a sleep for half a second before enabling the timer but this does not work. Maybe I need to somehow force a wait at the point before the timer is enabled and then carry on?

Sorry, I should have clarified. I did not mean that it would solve your current problem. I meant that it might cause future problems. The way you had it, is probably not a problem when the Interval is 0. However, if the Interval was something different, and you change it after enabling the timer, I am unsure what effect that would have. And I am an 'old woman' regarding keeping things simple and idiot proof. I know that whilst a timer was running, a loaded gun held to my head would not get me to change the Interval.

Dear Rothco,
Please can you explain what you intended to do with the timer? Because when I looked at the code you uploaded, I found out that there are no instructions inside the timer event handler.
So if you can explain what you intended to do we may be of help.

Roy,
I think it may be helpful to step back and explain a couple of features of the Visual Basic Timer control.

When you create a Timer, set the Timer.Interval, and enable the timer, you have started a process that will "fire" after the interval completes. However, in order for the timer to fire, your foreground process must not be running.

The timer will only trigger the _Timerevent procedure if the timer control can gain control of the computer; and it can't do that if your code is running.

You may have noticed that numerous posts have suggested that you execute the DoEvents method after enabling your timer. This method releases control. It allows any other process that is waiting to run to take control and execute. This normally includes other applications that are running on the computer. However, it also includes the timer.

In other words, it's not sufficient to execute DoEvents once, and then continue with your processing. You have two choices:

? If your code is running continuously - for example if you are executing a loop that is looking for some specific event before going on with code outside the loop, you must execute the DoEvents method in the loop

? If you code is running only as event procedures in forms and reports - so that your application executes only in response to events, you don't need to do much of anything. The timer will start the _Timer event procedure, just as other external actions (like the user clicking the mouse on a control) will trigger their event procedures

Let's look at what you have been experiencing. If you set a breakpoint in your foreground code, it stops running. The Visual Basic Interactive Development Environment (IDE) releases control until you interact with it. Therefore, the timer can gain control and begin its event procedure, which will hit the second breakpoint. Because the IDE is already in the "interact with the user" mode, it won't actually display the second breakpoint until you resume from the first breakpoint.

On the other hand, if you don't have the first breakpoint, your code keeps running. Therefore, the timer never gets to trigger its event procedure.

This implies that your code keeps on running. As I stated in the first of the two choices above, you need to insert a DoEvents in the code that continues running. You don't need a DoEvents in the code that enables the timer. You need one in the code that will be running when the timer fires.

Of course, a far better approach is to redesign your application so it doesn't keep running. Instead, design it so that it does whatever is necessary to create the user interface, and then releases control (basically, exits from the event procedure that displayed the form or whatever). Design your application so that everything is in the event procedures: everything is triggered by events, runs only as long as necessary, and then releases control. With this kind of structure, your application will have no difficulty responding to the timer. Your application will also be "well behaved" (it will play nicely with other applications). Look at whatever you're doing in that continuously executing loop, and figure out how to use events to recognize whatever you're testing for.

I have finally found the issue. As I said I launch a new form to hold the pictures and then exit from the routine.

However, on the picture form's deactivate event I set the timer.enabled to false. So the fact that the picture form loses focus is enough to turn off the timer! The reason for this was to ensure that the timer is turned off when the form is closed.

Glad it is sorted.
I like to minimize external locations (Forms etc), sending instructions to controls on other Forms for a coup[e of reasons -
1) I have a hunch that it is 'unclean' (not a clean solution)
2) It makes it very difficult to study code, and work out what is happening, and makes it difficult to debug.
(Don't take my naming conventions too serious, as I am just wishing to clarify what I mean)
Say we had 2 forms- frmMain and frmPictureForm
If I had a similar need, I would have a global variable in a Bas file.
Public b_frmPictureForm_Is_Running as Boolean
When frmPictureForm loads I would set it to True
(Actually you may have to set this to True in your frmMain, just prior to enabling the Timer)
When fmrPictureForm unloads I would set it to False
In the first lines of code in the Timer event, I would have -
If b_frmPictureForm_Is_Running = False then
'I would Set Timer.Enabled =False
Exit Sub '<=== EXIT
end if