Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer problem 1

Status
Not open for further replies.

mikeisvfp

Programmer
Mar 5, 2011
91
CA
Hello Experts,

I have a timer in form1 that refreshes my grid every x amount of seconds. I have another form2 that I call to update information displayed in grid of form1. I disable the timer when i call this form so the refresh does not interfere with my present form form2.

here is how i call my form2

Code:
LOCAL oform1 as Form
thisform.timer1.Enabled = .f.
DO FORM frmInvoice NAME oform1 LINKED WITH 0, invoicehdr.invoiceid NOSHOW
oform1.AutoCenter = .t.
oform1.Show(1) &&show modally
this.Refresh()

My question is, how do enablethe timer once I am done entering information in form2 ?
 
You need to arrange for Form2 to receive an object reference to Form1.

So, instead of this:

Code:
DO FORM frmInvoice NAME oform1 LINKED WITH 0, invoicehdr.invoiceid NOSHOW

do this:

Code:
DO FORM frmInvoice NAME oform1 LINKED WITH 0, invoicehdr.invoiceid[b], THISFORM[/b] NOSHOW

And in the Init of Form2, amend your LPARAMETERS statement to receive the object reference:

Code:
LPARAMETERS tnSomething, tnInvoiceID[b], toCaller[/b]

Then store toCaller in a custom property of Form2:

Code:
THISFORM.oCaller = toCaller

From now on, Form2 can address the timer in Form1 like so:

Code:
THISFORM.oCaller.Timer1.Enabled = .F.

However, a completely different approach (and probably a simpler one) would be not to place the timer on the form. Instead, instantiate it as a global object, which you can then address anywhere in the application:

Code:
PULIC goTimer
goTimer = CREATEOBJECT("timer")
 ....
 ....
goTimer.Enabled = .f.

Hope this makes sense.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Mike's idea is okay, but it would be much simpler, depending on what the moment "once I am done entering information in form2" is and when and where it happens.
If that means you come back to form1, then you can simply add the reactivation of the timer in your code, as last line:

Code:
[COLOR=#BABDB6]LOCAL oform1 as Form
thisform.timer1.Enabled = .f.
DO FORM frmInvoice NAME oform1 LINKED WITH 0, invoicehdr.invoiceid NOSHOW
oform1.AutoCenter = .t.
oform1.Show(1) &&show modally
this.Refresh()[/color] 
thisform.timer1.Enabled = .t.

Because it's as you write in the comment, you show the invoice form modally. so execution stops at the line oform1.Show(1), untile you exit the invoice form. And then you refresh and then you can also reactivate the timer.

So what's the problem, really? You have written the essential information yourself already. You just need to add the enabled = .t. to re-enable the timer at the end of that code. That's all. What are your thoughts? Why didn't you do it? How is your expectation of what happens at runtime? And why didn't you simply put a breakpoint there to see what happens in the debugger?

Bye, Olaf.
 
Another perhaps more to the point solution, is to make use of events. If you want a timer to pause, if a form isn't active, then you may disable the timer in the form.deactivate() event and activate it in the form.activate() event.

Code:
*Form Activate() event
ThisForm.Timer1.Enabled = .T.

Code:
*Form Deactivate() event
ThisForm.Timer1.Enabled = .F.

Now this will be handled, no matter why another form get's active.

Make yourself more comfortable with events happening anyway, if you know what events exist and when the happen, they are the ideal vehicle to react independant of what causes the event to happen.

Bye, Olaf.
 
Hello, sorry it took me so long to get back busy day today, anyway

@ MikeLewis, I went with your first solution and it works great. Thank you very much, however Id just like to make sure I understand what is happening here, I don't just like to copy and paste code.
so this is how I understand it, I basically created parameters in form2 and called this parameter from form1 once the parameter is picked the timer is disabled via THISFORM.oCaller.Timer1.Enabled = .F.
and then once the form is released this parameter no longer exists because form2 is released and therefore grid refresh() can presume, am I on the right track?

@ Olaf, I cant say I have tried your first solution and yes I will try and set step on to see how it actually works, as for your second solution provided I tried this before and it works fine accept if there are no records on the grid I get an error message. The reason for my error message is because in my refresh code is because I store the record number into memvar, I then refresh, and go back to stored record number so I don't lose position of my record pointer. I think it would become a little annoying if you have 20 records on the grid and your record pointer is at the 15th record and suddenly the grid refreshes and now you record pointer is at the top. hope you can agree with me on this one. However putting thisform.timer1.enabled = .t. in the Activate event causes confliction with my way of refreshing when there are no records in the grid, and the grid is filtered by DATE() so there are not many invoices from different dates on the grid.
 
Id just like to make sure I understand what is happening here, .... am I on the right track?

Yes, your analysis is correct.

However, I think you should also consider Olaf's first solution. If your only aim is to disable the timer while Form 2 is active, then that should work just as well, as far as I can see.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Well, MikeisVFP

there are so mayn unknown to us, eg what the timer and how it is onvolved with "this.refrsh", what "this" is, and what happens in this.Refresh().
But if it's about the timer to be reactivated, AFTER the second form finishes, then thisform.timer1.Enabled = .t.

And if that causes problems, then you need to address them where they happen.

For example:

If activating the timer is not always a good thing, eg if there are no records in a grid, then make that depend on Reccoun(thisform.grid.recordsource)>0, or have code in the timer event depend on the reccount or any other conditions, that mus be present.

You should understand what modal form means. As your problem was, "how do I enable the timer once I am done entering information in form2", the answer is right after you return from that form2. And that's the next line.

Also, you didn't answer my in direct question, what the moment "once I am done entering information in form2" is. What is it?
If that moment occurs, WHILE form2 runs, then form2 needs to have a reference to form1, like you have using Mike Lewis' solution. And then you can enable the timer from the invoice form, no matter where exactly. That form then knows the other form. The disadvantage of that is, it will of course only work, if the second form would be called by the first form. You introduce a dependency this way.

Keep it simple. The moment you oform1.Show(1) &&show modally, the debugger will switch to that form init, go through all initialisation, the form will show, and finally the trace window goes back to the line oform1.Show(1) &&show modally, it won't go to the next line, unless the form you called finishes. Therefore the timer still is disabled all this time, until the form closes. Only then "this.refresh" runs, and the re-enabling of the timer. Only then, note before. That's what modal means.

If you still have a problem with refresh events, the grid erroring, then disabling the timer didn't help, you bark up the wrong tree. Find out where the problem really comes from. Perhaps your other form uses the default datasession, meaning the current datasession. And you influence workareas, wou should influence, and therefor your refresh or grid errors or misbehaves. Perhaps the solution is even simpler, and you only need to make the invoice form have it's own private datasession.

I have really no doubt, I have answered your question, adding that single line does enable the timer, after the other form finishes. If that doesn't solve your problem, it doesn't mean that line reactivates the timer too early. It means, that either re-enabling the timer after the other form finishes still is too early, or you have a totally different problem, eg finishing the other form, closes all workareas and tables. Or the timer is not at all involved in your problem, you only think it is.

Find out where the problem really is, and I'm sure we can solve that.

Bye, Olaf.
 
[shadeshappy]Hello Olaf,

There really is no problem, i really just didnt know how to get it to work. But you and Mike provided me with so many different solutions, and they all work thank you , I just finished trying your first solution and it seems to be working aswell.

Thank you once again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top