Deleting all Timers VB.net - vb.net

I create timers within a class
Dim timer As New Timer
timer.Enabled = True
timer.Interval = 1000
timer.Tag = "TimeslipTimer_" & timeslip.id
AddHandler timer.Tick, AddressOf GlobalTimerTick
timer.Start()
The problem i have is how can I delete those if I needed?
At the moment I was looking to add timers to a list, similar to below, but i didnt work out
dim timers as new list(of Timer)
For Each c As Timer In Timers
c.Dispose()
Timers.Remove(c)
Next

When removing from a list the list must be accessed in reverse order. Also remember to remove the handler.
Public Class Form1
Private WithEvents timer As Timer
Private timers As New List(Of Timer)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timer = New Timer
timers.Add(timer)
timer.Enabled = True
timer.Interval = 1000
'timer.Tag = "TimeslipTimer_" & timeslip.id
AddHandler timer.Tick, AddressOf GlobalTimerTick
timer.Start()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For x As Integer = timers.Count - 1 To 0 Step -1
Dim t As Timer = timers(x)
RemoveHandler t.Tick, AddressOf GlobalTimerTick
t.Stop()
t.Dispose()
timers.RemoveAt(x)
Next
End Sub
Private Sub GlobalTimerTick(sender As Object, e As EventArgs)
Debug.WriteLine("TICK")
End Sub
End Class

Related

How To Auto Refresh Xml page in VB.NET?

Trying to auto refresh xml page with timer but i see the node but the timer does not work. I post my code if you can find the problem thanks a lot in advance.
Private Sub TextBox1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dom As New Xml.XmlDocument
dom.Load("http://69.175.13.131:8050/stats")
TextBox1.Clear()
Dim monitorid As String = String.Empty
For Each node As Xml.XmlNode In dom.SelectNodes("//SHOUTCASTSERVER/SONGTITLE")
monitorid = node.InnerText
TextBox1.Text=(monitorid)
Next
Dim timer As New Timer()
timer.Interval = 20000
AddHandler timer.Tick, AddressOf TextBox1.Refresh
timer.Start()
End Sub
The Control.Refresh method is only to redraw the control, not update its contents (unless you write code in an unexpected place to do that).
If you put the code to retrieve the data in a separate method, you can make it a bit tidier and easier to see what is happening in the code. Something like:
Public Class Form1
Dim tim As Timer
Sub ShowCurrentSongTitle(sender As Object, e As EventArgs)
Dim dom As New Xml.XmlDocument
dom.Load("http://69.175.13.131:8050/stats")
Dim node As Xml.XmlNode = dom.SelectSingleNode("//SHOUTCASTSERVER/SONGTITLE")
Dim monitorid As String = node.InnerText
TextBox1.Text = monitorid
TextBox1.SelectionStart = monitorid.Length
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ShowCurrentSongTitle(Nothing, EventArgs.Empty)
tim = New Timer With {.Interval = 20000}
AddHandler tim.Tick, AddressOf ShowCurrentSongTitle
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' tidy up...
RemoveHandler tim.Tick, AddressOf ShowCurrentSongTitle
End Sub
End Class

Auto refresh form every 30 seconds vb.net

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.
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
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
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

Closing application after X mins of inactivity in Visual Basic

I have an application that I need to close after it is inactive or not used. I have written the below code and am getting
System.InvalidOperationException in windowsBase.dll
Do you have any idea how I can get this done? I need the application not to "freeze" using some of the threading timers.
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 9000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Close()
End Sub
Try :
Dim app as Application
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
app.Exit()
End Sub
That seems to have worked for me.
Here is my full code on a blank form I made to test your issue. I get the smae exception you get trying to use Me.Close() but my Application.Exit() and app.Exit() work without a problem.
Public Class Form1
Dim app As Application
Private Sub MainWindow_Loaded(sender As Object, e As EventArgs) Handles Me.Load
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 5000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
'Environment.Exit(0) - closes without a problem
'Application.Exit() - closes without a problem
'closes without a problem.
app.Exit()
'Me.Close() - System.InvalidOperationException
End Sub
End Class

VB.NET Create and Close Forms after Certain Time

I could easily close form after few seconds; but when I want to close many forms one after another, same sequence as they were "created"; I could not figure it out:
The main form code is as below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numberOfForms As Integer = 10
For open = 1 To numberOfForms
TestClosing()
Next
End Sub
The module code that I am trying to create then close forms after few seconds is as below:
Imports System.Timers
Module ClosingModule
Sub TestClosing()
Dim frmNew As New Form
frmNew.Show()
Dim tmr As New System.Timers.Timer()
tmr.Interval = 3000
tmr.Enabled = True
tmr.Start()
End Sub
End Module
I started a timer, but all the methods I tried to close the form is same sequence they were created; were not successful;
Help appreciated; and thanks in advance.
Add the Timer to the Forms you are creating, start it when the Form is created that way they will be closed in the same order that they were created. I also added an incremental delay to that the order of closing is more evident.
Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim numberOfForms As Integer = 10
For open = 1 To numberOfForms
Dim frmNew As New Form2
frmNew.Text = open.ToString
frmNew.Show()
Next
End Sub
Form2
Public Class Form2
Dim myTimer As New Timer()
Private Sub myTimer_Tick(sender As System.Object, e As System.EventArgs)
myTimer.Stop()
Me.Close()
End Sub
Private Sub Form2_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
myTimer.Interval = 1000 * CInt(Me.Text)
AddHandler myTimer.Tick, AddressOf myTimer_Tick
myTimer.Start()
End Sub
End Class
Added code in module to do the same thing:
Imports System.Timers
Module ClosingModule
Sub TestClosing(multiplier As Integer)
Dim frmNew As New Form
frmNew.Show()
Dim tmr As New System.Timers.Timer()
AddHandler tmr.Elapsed, AddressOf Timer_Elapsed 'Add Handler to New Timer
tmr.SynchronizingObject = frmNew 'Synchronize Timer to newly created form
tmr.Interval = 1000 * multiplier
tmr.Enabled = True
tmr.Start()
End Sub
Public Sub Timer_Elapsed(sender As Object, e As ElapsedEventArgs)
Dim tmr As System.Timers.Timer = DirectCast(sender, System.Timers.Timer)
tmr.Stop() 'Stop Timer
DirectCast(tmr.SynchronizingObject, Form).Close() 'Get Form Timer was synchronized with and close it
tmr.SynchronizingObject = Nothing 'Remove Form reference from timer
RemoveHandler tmr.Elapsed, AddressOf Timer_Elapsed 'Remove Handler from Timer
End Sub

How to create multiple timer handler in vb.net

I have to create multiple timer "n" times with handler.
Which will be stored for a row in my DataGrid.
For each row there will be a timer that works seperately.
What I thought of looks like:
Private Sub CreateTimer()
Dim tmr As New Timer
tmr.Interval = 1000 '1 Second
tmr.Enabled = True
AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub
'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(TheTimer as Timer, ByVal sender As Object, ByVal e As EventArgs)
mynumber = mynumber + 1
With DataGridView1
.Rows(n).Cells(4).Value = mynumber " saniye"
End With
End Sub
So how can I achieve this?
I believe Tag property of Timer would work beautifully in your case. I don't have my IDE currently, but the following snippet should give you the idea.
Private Sub CreateTimer()
Dim tmr As New Timer
tmr.Interval = 1000 '1 Second
tmr.Enabled = True
tmr.Tag = ROW_INDEX
AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub
'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(ByVal sender As Object, ByVal e As EventArgs)
mynumber = sender.Tag
With DataGridView1
.Rows(n).Cells(4).Value = mynumber " saniye"
End With
End Sub