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
Related
been trying to follow this guide on how to make picture boxes change colour depending on if the ping has been successful or not. I have some test ip's in a CSV file that the program imports
but I keep getting a exception error. I have 3 picture boxes on the page currently defined from picturebox2 - 4
the code for the whole page is below, any suggestions or fixes would be greatly appreciated
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Threading
Imports System.IO
Imports System.Timers
Public Class Central
Dim pingTable As DataTable = New DataTable()
Dim ipAddress As List(Of String) = New List(Of String)
Dim pictureBoxList As List(Of PictureBox) = New List(Of PictureBox)
Dim timer As Timers.Timer
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
timer = New Timers.Timer
timer.Interval = 20000
timer.Enabled = True
AddHandler timer.Elapsed, AddressOf OnElapsedTime
End Sub
Private Sub OnElapsedTime(ByVal sender As Object, ByVal e As ElapsedEventArgs)
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub FillPingTable()
pingTable.Columns.Add("ip", GetType(String))
pingTable.Columns.Add("picturebox", GetType(String))
pingTable.Rows.Add()
For i As Integer = 0 To ipAddress.Count - 1
pingTable.Rows.Add(ipAddress(i), pictureBoxList(i))
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
Scot.Show()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Hide()
Home.Show()
End Sub
Private Sub central_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using reader = New StreamReader("A:\IpAddress.csv")
While Not reader.EndOfStream
Dim line = reader.ReadLine
Dim values = line.Split(ChrW(10))
ipAddress.Add(values(0))
End While
For i As Integer = 1 To 2
pictureBoxList.Add(CType(Controls.Find("PictureBox" & i, True)(0), PictureBox))
FillPingTable()
BackgroundWorker1.RunWorkerAsync()
Next
End Using
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Thread.Sleep(500)
Parallel.For(0, ipAddress.Count(), Sub(i, loopState)
Dim ping As New Ping()
Dim pingReply As PingReply = ping.Send(ipAddress(i).ToString())
Me.BeginInvoke(CType(Sub()
pictureBoxList(i).SizeMode = PictureBoxSizeMode.Zoom
pictureBoxList(i).BackColor = If(pingReply.Status = IPStatus.Success, Color.Green, Color.Red)
End Sub, Action))
End Sub)
End Sub
End Class
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
I call my page every 20 seconds but when i call my page i see a flash. Is it possible to avoid the flickering when the page load?
Private Sub WebBrowser1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(New Uri("http://metalrockpopradio.caramania.com/tuneefy4.php"))
Dim timer As New Timer()
timer.Interval = 20000
AddHandler timer.Tick, AddressOf WebBrowser1.Refresh
timer.Start()
You can do something like this:
Create a Form (here called Form1)
add a PictureBox (here, called PictureBox1) to it
set the PictureBox size to what you want and set its SizeMode = Zoom
paste the following code inside the Form's code partial file (F7 on the Form)
Imports System.Net
Imports System.Windows.Forms
Public Class Form1
Dim timer As System.Windows.Forms.Timer
Private client As WebClient = New WebClient()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshImage(Nothing, Nothing)
timer = New System.Windows.Forms.Timer()
AddHandler timer.Tick, AddressOf Me.RefreshImage
timer.Interval = 5000
timer.Start()
End Sub
Protected Sub RefreshImage(sender As Object, e As EventArgs)
Dim HttpResource As String = client.DownloadString("http://metalrockpopradio.caramania.com/tuneefy4.php")
Dim ParseStart As Integer = HttpResource.IndexOf("=") + 2
HttpResource = HttpResource.Substring(ParseStart, HttpResource.LastIndexOf(ChrW(34)) - ParseStart)
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
PictureBox1.Load(HttpResource)
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
client.Dispose()
timer.Dispose()
End Sub
End Class
Have two of them. When the new reload is done, show the new control and hide the old.
Alternatively, use AJAX or similar on your page to reload content.
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
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