0

I want to have a form opened all day but I want to auto refresh it every 30 seconds. I am using this code:

    Private Sub tempo(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim timer = New Timer
        timer.Interval = 30 * 1000
        AddHandler timer.Tick, AddressOf Form12_Load
        timer.Start()

    End Sub


    Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ...
    End sub

But it isn't working. Do you know what I am doing wrong? Thank you.

  • 1
    Is it a web application? If yes, then you can't use a timer. The refresh needs to be done on the client (javascript, updatepanel, ...) – the_lotus Nov 07 '18 at 15:21
  • @the_lotus no, it is a windows application. I used this in another programm and it worked. I can't find what's going wrong. – Alexandra Macedo Lopes Nov 07 '18 at 15:23
  • What is it not doing? A little more detail would be useful. – Brian M Stafford Nov 07 '18 at 15:24
  • @BrianMStafford It isn't refreshing my form. – Alexandra Macedo Lopes Nov 07 '18 at 15:27
  • 1
    @AlexandraMacedoLopes I copied your code into a new project and `Form12_Load` is called every 30 seconds like you want. The problem must be elsewhere in your code. – MatSnow Nov 07 '18 at 15:54
  • I agree with @MatSnow. This seems to work fine. – Brian M Stafford Nov 07 '18 at 15:55
  • @MatSnow Brian M Stafford do you have any idea what it could be?? – Alexandra Macedo Lopes Nov 07 '18 at 15:59
  • @AlexandraMacedoLopes It would help to know (or better see) what happens inside `Form12_Load`. Please edit the question and add the necessary information. – MatSnow Nov 07 '18 at 16:02
  • @AlexandraMacedoLopes - Are there two forms in play here? There are two load events. – dbasnett Nov 07 '18 at 20:40
  • Add a Timer to the designer. It will appear at the bottom of the designer in the components area. Set the interval property in the Properties window. Double click the timer and your stub for the Tick event will appear in the code window. Add your refresh code. Add Timer1.Start() to your Load event. – Mary Nov 07 '18 at 22:09
  • I suggest that you put the updating code from `Form12_Load` into a separate method, then you can call it from the `MyBase.Load` event handler and the timer tick handler. Also, use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) as it might point out something wrong that we can't see. – Andrew Morton Nov 07 '18 at 22:46

3 Answers3

0

My 0.02, should not need much explanation:

Private WithEvents clock As New Timers.Timer

Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    With clock
        .Interval = 30000
        .AutoReset = True
        .Enabled = True
        .Start()
    End With
End Sub

Private Sub clock_tick() Handles clock.Elapsed
    Me.BeginInvoke(Sub()
                       Me.Refresh()
                   End Sub)
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • 1
    The elapsed event is not running on the UI so an error will occur. I have edited your code. – dbasnett Nov 07 '18 at 17:04
  • @dbasnett : It's better to just set [`Timer.SynchronizingObject`](https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.synchronizingobject?view=netframework-4.7.2#System_Timers_Timer_SynchronizingObject) to the current form. Then you don't have to invoke on your own. :) – Visual Vincent Nov 07 '18 at 17:11
  • 1
    @AlessandroMandelli : When performing UI-related work at certain intervals it is preferred to use a [`System.Windows.Forms.Timer`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer) instead as it already runs on the UI thread with quite a small overhead. (: – Visual Vincent Nov 07 '18 at 17:13
  • @VisualVincent - for this question I agree with both points, especially when all the OP is doing is refreshing the form, though I wonder what we are missing that requires this. Is there some big loop that hasn't been shown? – dbasnett Nov 07 '18 at 17:16
  • @dbasnett : If I had to guess the OP is doing something like fetching new data from a database/web server every 30 sec, but we've been fooled before and things might not be at all what they initially seem to be. The lack of information makes it hard to suggest any better ways (if there are any), so we'll just have to trust that the OP knows what she is doing. ;) – Visual Vincent Nov 07 '18 at 17:19
  • Yeah, I was under the impression I missed something, thanks. – Alessandro Mandelli Nov 07 '18 at 18:40
0

Firsly you need to take timer. after that you select time interval. how much time you want to refresh from load page. in my case im creating one function form load in side formload function is nothing you just type your commands. want to update on load page ===** MyBase.Update()** im using that. 'Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick ' formload() ' ref_screen() 'End Sub

harish
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 15:18
-2

Hi you have to call the form_load event from the tick-timer event:

Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim timer = New Timer
    timer.Interval = 30 * 1000
    AddHandler timer.Tick, AddressOf timer_Tick
    timer.Start()
End sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)   
    Form12_Load(me,nothing)
End Sub 
gsimo83
  • 109
  • 2
  • 9
  • 1
    Plus, "now the meaning of the source-code is clear and obvious." It's instantly apparent that the function is a timer-tick handler, and that what it *(presently ...)* does is to load the form. Some day, inevitably, you'll want to do something else during that timer-tick, and now that will also be obvious and easy. gsimo's version is what any programmer would *expect* to see, and that's an important consideration too. – Mike Robinson Nov 07 '18 at 15:30
  • 2
    This will result in doubling the number of timers every 30 seconds, probably not what the OP is desiring. – Brian M Stafford Nov 07 '18 at 15:42
  • Agree with Brian, this doesn't look like the right solution at all. It will endlessly create more timers – Benno Nov 07 '18 at 15:47
  • @gsimo83 it only refreshes the form once. – Alexandra Macedo Lopes Nov 07 '18 at 15:51