WayneSpangler

I have a timer on form1 with the timer_tick set up like this:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        teaTime = False
        MessageBox.Show("End timer")
    End Sub

In a module I have:

    Public Sub Transition1()
        Form1.Timer1.Enabled = True
        teaTime = True
        Dim st As Integer = 0
        Do While teaTime
            Form1.TextBox1.Text = CStr(st)
            Form1.TextBox1.Refresh()
            st += 1
        Loop
        Form1.Timer1.Enabled = False
    End Sub

Why doesn't the timer1_tick event not fire. Is it because I am enableing and disableing it from a module

The st integer is in there to see it the loop was executing. Has no baring on the program. The do loop is executing but the timer won't fire.

 The timer inteveral is set for 3000 miliseconds and I have waited for 30 or 40 seconds.

 




Re: Visual Basic Express Edition Timer won't fire

ReneeC

Is it enabled to begin with






Re: Visual Basic Express Edition Timer won't fire

ahmedilyas

make sure you started the timer:

Form1.Timer1.Start()

however it is not recommended to go through the default instance as you are doing, it is better to have an existing reference of that object you are going to be accessing, form1. However lets get this working and should you require more info we will be glad to help :-)

Be sure you did start the timer






Re: Visual Basic Express Edition Timer won't fire

Dave299

If you put an Application.DoEvents in your loop you should find it works fine.



Re: Visual Basic Express Edition Timer won't fire

ahmedilyas

you should avoid calling Application.DoEvents() as its bad practice. It can cause some unpredictable results some times and processing all messages in the queue when it shouldnt need to be at the time. you really ought to find the core problem first and fix it :-)




Re: Visual Basic Express Edition Timer won't fire

WayneSpangler

Renee:
I didn't have it enabled in design but I tried that but it didn't fire the event.

Ahmedilyas:
I tried referenceing it thuslly:

Made the sub call like this:

 Transition1(Timer1)

Changed my subroutine to this:

    Public Sub Transition1(ByRef Timer2 As Timer)
        Timer2.Start()
        Timer2.Interval = 1000
        Timer2.Enabled = True
        teaTime = True
        Dim st As Integer = 0
        Do While teaTime
            Form1.TextBox1.Text = CStr(st)
            Form1.TextBox1.Refresh()
            st += 1
        Loop
        Timer2.Enabled = False
        Timer2.Stop()
    End Sub

The timer_tick still doesn't fire. I though I understood the timer as many times as I used it in vb6. It must have some different characteristics vb net.

Edit: The Application.DoEvents does work. Maybe I should put in a delay of somesort here.






Re: Visual Basic Express Edition Timer won't fire

ahmedilyas

you did implement the timer tick event right (it happens, sometimes we forget or the event is there but not registered for the component)




Re: Visual Basic Express Edition Timer won't fire

nobugz

Dave299 has the correct answer. You are running in a loop and not dispatching Windows messages. That's why you need to call TextBox1.Refresh() to see anything. Calling Application.DoEvents() inside the loop will ensure that the timer's Tick event can run. And that TextBox1 will paint itself.

Whether or not DoEvents is incorrect is beside the point. You should never use a loop that waits for an event to occur in a Windows program.





Re: Visual Basic Express Edition Timer won't fire

WayneSpangler

I have heard for some time now not to use DoEvents but I have never seen a good reason not to use it. The sub runs like I want it to do using DoEvents. Will a delay of some sort work inplace of the DoEvents I want to code the right way. I just started using Strict On and boy have I got a lot of corrections in my programs. We didn't have Strict in VB6.




Re: Visual Basic Express Edition Timer won't fire

WayneSpangler

This is what I ended up doing.

Public Sub Transition1(ByRef Timer2 As Timer)
Dim stTime As Date = Now.AddSeconds(3)
Form1.TextBox1.Text = Now.Second.ToString
Do While stTime.Second > Now.Second

Loop
Form1.TextBox2.Text = Now.Second.ToString

End Sub

It works without using a timer. Your thoughts........






Re: Visual Basic Express Edition Timer won't fire

nobugz

There is nothing inherently wrong with DoEvents, it is just difficult to do it 100% correctly. It got a bad reputation from the VB6 days when many programmers used it to "multi-task" without regard for what could go wrong. The .NET way is to use threads. Doing threading 100% correctly is *very* difficult. As opposed to DoEvents which is just a little difficult. That always buys me a long string of hate posts.

Anyhoo, the one thing to watch out for when you use DoEvents is to keep in mind that you've got code running in a loop. You must make sure that the user cannot close a form or start the same loop again. In the first case, your program keeps running but without a window. In the second case, you've got your loop running twice with the first one suspended, probably making a big mess of whatever your doing in the loop. This is all easy to avoid by using a boolean field in your class/form (like TeaTime) and/or by disabling controls.

In your particular case, your program will never quit when you close the form while it is inside the loop. The window is gone so you'll never get the timer event. You'll have to kill it by hand with Task Manager. A classic DoEvents problem and the very reason it got a bad reputation. The fix is easy: set TeaTime to false in the FormClosing event.





Re: Visual Basic Express Edition Timer won't fire

Chris Dunaway

Depending on what you have in the Do While loop, you're going to peg your processor at 100% doing that.

What are you trying to accomplish Perhaps a better way can be suggested.






Re: Visual Basic Express Edition Timer won't fire

ReneeC

 

I think the OP's solution is far worse that Application.Doevents. NEVER make a compute bound loop for delay. Nothing else can get compute resources.

This is true for so many reasons that it's hard to list. My machine changes core clock speeds depending on the compute demand. That loop will actually cause chips to heat up. It chews up compute resources and for that reason alone it's bad engineering practice. Fix the problem which is that your timer is not firing. Don exacerbate it by either sleep or compute bound loops. Spotty is quite right about sleeping during a loop.

I never use a designer based timer. I always declare it as"

Protected Friend withevents tmr as new timer

 

 

 






Re: Visual Basic Express Edition Timer won't fire

WayneSpangler

OK, what I am doing is creating a transition from one file to another. One idea is to transfer fileA to fileB one random pixel at a time. I didn't want the transfer to go on for a long period of time so I wanted to check when 2 seconds had passed and then transfer the hold file at one time. Any suggestions for checking when 2 seconds have passed would be appreciated.

Note also that I may use your answer on other peoples question.

I am learning from this thread. Speaking of which I am going to have to learn threading. Someone got a good tutorial






Re: Visual Basic Express Edition Timer won't fire

ReneeC

 

Wayne,

How big is the file

What do you mean transfer the data

Why not just rename file A and B That will "transfer the data".

Actually the help files have a pretty good discussion on threading. Multi-threading on files can be dangerous for your data unless you really know what you are doing.

Get the timer to work. It's not rocket science. Put a debugger break point at the timer event. if the break point doesn't break you know the timer is not being started.