Timer will not start a second time - vb.net

I've got a function:
Private Sub UpdateSch()
Threading.Thread.Sleep(50)
Dim i As Integer = 1
While i = 1
Try
If DataGridView1.Rows.Count > 1 Then
DataGridView1.Rows.Clear()
End If
Using stream As System.IO.FileStream = System.IO.File.OpenRead("Z:\\SchData.txt")
Using reader As New System.IO.StreamReader(stream)
Dim line As String = reader.ReadLine()
While (line IsNot Nothing)
Dim columns = line.Split(";")
line = reader.ReadLine()
Dim index = Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(index).SetValues(columns)
End While
End Using
End Using
Button88.Enabled = True
DataGridView1.CurrentCell = DataGridView1.Rows(rowIndex).Cells(colIndex)
i = 0
Catch ex As Exception
Threading.Thread.Sleep(50)
End Try
End While
'Check for local updating
If updatingSch = False Then
DataGridView1.Enabled = False
LockWarning1.Visible = True
lockVar1 = 0
LockTimer1.Start()
Else
updatingSch = False
End If
End Sub
And then I've got a timer:
Private Sub LockTimer1_Tick(sender As Object, e As EventArgs) Handles LockTimer1.Tick
LockWarning1.Visible = False
DataGridView1.Enabled = True
LockTimer1.Stop()
End Sub
The function updateSch is called at form load and whenever the file is changed. It locks the DataGridView, starts the timer which runs for 10 sec, and then unlocks the Datagridview. This all works on load, but when it is called again it locks and never unlocks. The second time around the timer is never started. (I put a break point on the "LockTimer1.Start()" and it is executed the second time, but the LockTimer_Tick event doesn't fire after that)

I've found a soution to my problem. For the life of me I can't figure out why my previous code won't work. What I was able to get working was a Systems.Timers.Timer (instead of System.Windows.Forms.Timer). My code creating the timer looks like this:
Dim LockTimer1 As New System.Timers.Timer()
LockTimer1.Interval = 10000
LockTimer1.AutoReset = False 'Run timer only once
LockTimer1.Start()
AddHandler LockTimer1.Elapsed, AddressOf LockTimer1_Tick
And then the function LockTimer1_tick:
Private Sub LockTimer1_Tick(ByVal sender As Object, ByVal e As ElapsedEventArgs)
LockWarning1.Visible = False
DataGridView1.Enabled = True
Button88.Enabled = True
Button1.Enabled = True
End Sub

Related

SevenZipSharp: Events not firing

I have the following problem with SevenZipSharp. I want to compress a list(of String) containing filenames with full path. My code works fine, but only the last event(zip.CompressionFinished) is firing. Neither fFileCompressionStarted nor fCompressing is firing. What am I doing wrong?
Even if I set breakpoints in the event-subs or type "Stop", nothing happens.
Here is my code:
Dim working As Boolean
Private Sub start()
Dim zip As New SevenZipCompressor
zip.ArchiveFormat = OutArchiveFormat.SevenZip
zip.CompressionMode = CompressionMode.Create
zip.CompressionLevel = CompressionLevel.Fast
zip.CompressionMethod = CompressionMethod.Lzma2
zip.DirectoryStructure = True
zip.FastCompression = True
zip.IncludeEmptyDirectories = True
zip.PreserveDirectoryRoot = True
zip.TempFolderPath = System.IO.Path.GetTempPath()
AddHandler zip.FileCompressionStarted, AddressOf fFileCompressionStarted
AddHandler zip.Compressing, AddressOf fCompressing
AddHandler zip.CompressionFinished, AddressOf Compress_Finished
working = True
Label10.Text = "Startup..."
Application.DoEvents()
zip.BeginCompressFiles(filename, flist.ToArray)
While working = True
Threading.Thread.Sleep(250)
Application.DoEvents()
End While
End Sub
Private Sub fFileCompressionStarted(ByVal sender As Object, ByVal e As SevenZip.FileNameEventArgs)
Debug.Print(("Compressing " + e.FileName + e.PercentDone.ToString))
Label10.Text = e.FileName
MsVistaProgressBar1.Value = e.PercentDone
Application.DoEvents()
End Sub
Private Sub fCompressing(sender As Object, e As SevenZip.ProgressEventArgs)
MsVistaProgressBar1.Value = e.PercentDone
Application.DoEvents()
End Sub
Private Sub Compress_Finished(sender As Object, e As EventArgs)
MsVistaProgressBar1.Value = 0
Label10.Text = "Ready."
working = False
Application.DoEvents()
End Sub
Sorry to dig up this old question, but I was struggling with the same issue yesterday. I found that setting FastCompression to False will cause the events to fire properly.

Visual Basic - start stopwatch for measuring overtime after a given amount of minutes

I'm starting my first steps into Visual Basic and trying to create sort of a stopwatch.
My design is the following:
The idea behind is to create a tool to support a debate. Persons gets a certain time (7 minutes) for presenting their topic and after that time there is room for interactive conversation (Q&A) set to 13 minutes. The idea is that after 7 minutes a buzzer sounds to stop the presenting time and go over to the interactive part. And after 20 minutes a 2nd stopwatch starts with a buzzer and a red flashing background to indicate that session needs to be terminated.
This is what I already got and I'm pretty shure it can be coded otherwise and maybe easier.
I already got it to the first working stopwatch but I don't get the rest working:
Public Class Form1
Private Hundredths As Integer = 0
Private Seconds As Integer = 0
Private Minutes As Integer = 0
Private Hours As Integer = 0
Private OvertimeHundredths As Integer = 0
Private OvertimeSeconds As Integer = 0
Private OvertimeMinutes As Integer = 0
Private OvertimeHours As Integer = 0
Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
If Timer1.Enabled Then
Timer1.Stop()
StartBtn.Text = "START"
Return
End If
If Not Timer1.Enabled Then
Timer1.Start()
StartBtn.Text = "STOP"
Return
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Hundredths += 1
HundredthsTxB.Text = Hundredths.ToString
SecondsTxB.Text = Seconds.ToString
MinutesTxB.Text = Minutes.ToString
HoursTxB.Text = Hours.ToString
If Hundredths = 10 Then
Seconds += 1
Hundredths = 0
End If
If Seconds = 60 Then
Minutes += 1
Seconds = 0
End If
If Minutes = 60 Then
Hours += 1
Minutes = 0
End If
If Hours = 24 Then
Timer1.Stop()
End If
End Sub
Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click
Hundredths = 0
Seconds = 0
Minutes = 0
Hours = 0
HundredthsTxB.Text = Hundredths.ToString
SecondsTxB.Text = Seconds.ToString
MinutesTxB.Text = Minutes.ToString
HoursTxB.Text = Hours.ToString
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
OvertimeHundredths += 1
OvertimeHundredthsTxB.Text = OvertimeHundredths.ToString
OvertimeSecondsTxB.Text = OvertimeSeconds.ToString
OvertimeMinutesTxB.Text = OvertimeMinutes.ToString
OvertimeHoursTxB.Text = OvertimeHours.ToString
If OvertimeHundredths = 10 Then
OvertimeSeconds += 1
OvertimeHundredths = 0
End If
If OvertimeSeconds = 60 Then
OvertimeMinutes += 1
OvertimeSeconds = 0
End If
If OvertimeMinutes = 60 Then
OvertimeHours += 1
OvertimeMinutes = 0
End If
If OvertimeHours = 24 Then
Timer2.Stop()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Timer1.Interval = 2 Then
Timer2.Start()
End If
End Sub
End Class
So any help is welcome. Also I'm keen on learning on how other guru's are thinking and coding.
Instead of using timers, I would use a Stopwatch. This way there is less messing around adding numbers etc. The code below accomplishes what you want apart from sounding a buzzer as I'm presuming you want to implement it your own. It looks worse than it is, but seems to work ok. See code comments for a little explanation
Public Class Form1
Private PresentationTimer As New Stopwatch
Private InteractiveTimer As New Stopwatch
Private PresentationMinutes As Integer
Private InteractiveMinutes As Integer
'When this timer is enabled, it keeps track of the two stopwatches and checks
'that they're within the alloted time.
'
'When the presentation timer has expired, that one is stopped and the interactive
'timer is started
'
'When the interactive timer has expired, the timer for background flashing starts.
'To stop the flashing, click the reset button on the form
Private Sub UiUpdateTimer_Tick(sender As Object, e As EventArgs) Handles uiUpdateTimer.Tick
UpdateUI()
If PresentationTimer.IsRunning And IsTimeExpired(PresentationTimer, PresentationMinutes) Then
PresentationTimer.Stop()
InteractiveTimer.Start()
'add code here for buzzer to show end of presentation time
End If
If InteractiveTimer.IsRunning And IsTimeExpired(InteractiveTimer, InteractiveMinutes) Then
InteractiveTimer.Stop()
BackgroundFlashTimer.Enabled = True
'add code here for buzzer to show end of interactive time
StartBtn.Text = "START"
End If
End Sub
'checks if the timer passed as a parameter is expired by comparing
'the number of elapsed minutes with the number of minutes passed in
'the second parameter
Private Function IsTimeExpired(tmr As Stopwatch, mins As Integer) As Boolean
If tmr.Elapsed.Minutes >= mins Then
Return True
Else
Return False
End If
End Function
'simply updates all the textboxes with the stopwatch values.
Private Sub UpdateUI()
HundredthsTxB.Text = Int(PresentationTimer.Elapsed.Milliseconds / 10).ToString
SecondsTxB.Text = PresentationTimer.Elapsed.Seconds.ToString
MinutesTxB.Text = PresentationTimer.Elapsed.Minutes.ToString
HoursTxB.Text = PresentationTimer.Elapsed.Hours.ToString
OvertimeHundredthsTxB.Text = Int(InteractiveTimer.Elapsed.Milliseconds / 10).ToString
OvertimeSecondsTxB.Text = InteractiveTimer.Elapsed.Seconds.ToString
OvertimeMinutesTxB.Text = InteractiveTimer.Elapsed.Minutes.ToString
overtimeHoursTxB.Text = InteractiveTimer.Elapsed.Hours.ToString
End Sub
Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
'if either timer is running, stop them both and exit this sub
If PresentationTimer.IsRunning Or InteractiveTimer.IsRunning Then
PresentationTimer.Stop()
InteractiveTimer.Stop()
StartBtn.Text = "START"
uiUpdateTimer.Enabled = False
Exit Sub
End If
'if the presentation timer hasn't run yet, check if the minutes values are
'valid and start it
If PresentationTimer.ElapsedMilliseconds = 0 Then
PresentationMinutes = CInt(Val(PresentationMinutesTxB.Text))
InteractiveMinutes = CInt(Val(InteractiveMinutesTxB.Text))
'if the minutes values are't valid show messagebox and exit this sub
If PresentationMinutes = 0 Or InteractiveMinutes = 0 Then
MessageBox.Show("Please enter valid Presentation and Interactive times.")
Exit Sub
End If
'if minutes values are ok start the presentation timer
uiUpdateTimer.Enabled = True
PresentationTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
'if the presentation timer has been running, but is currently stopped,
'continue the timer and exit this sub
If Not PresentationTimer.IsRunning And PresentationTimer.ElapsedMilliseconds > 0 Then
uiUpdateTimer.Enabled = True
PresentationTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
'if the interactive timer has been running,but is currently stopped,
'continue the timer and exit this sub
If Not InteractiveTimer.IsRunning And InteractiveTimer.ElapsedMilliseconds > 0 Then
uiUpdateTimer.Enabled = True
InteractiveTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
End Sub
Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click
PresentationTimer.Reset()
InteractiveTimer.Reset()
uiUpdateTimer.Enabled = False
StartBtn.Enabled = True
StartBtn.Text = "START"
UpdateUI()
BackgroundFlashTimer.Enabled = False
Me.BackColor = defaultBackColor
End Sub
Private Sub BackgroundFlashTimer_Tick(sender As Object, e As EventArgs) Handles BackgroundFlashTimer.Tick
StartBtn.Enabled = False
If Me.BackColor = DefaultBackColor Then
Me.BackColor = Color.Red
Else
Me.BackColor = DefaultBackColor
End If
Me.Update()
End Sub
End Class
This was my final solution based on David Wilson's contribution:
Public Class Form1
Private PresentationTimer As New Stopwatch
Private InteractiveTimer As New Stopwatch
Private ExtraTimeTimer As New Stopwatch
Private PresentationMinutes As Integer
Private InteractiveMinutes As Integer
'When this timer is enabled, it keeps track of the two stopwatches and checks
'that they're within the alloted time.
'
'When the presentation timer has expired, that one is stopped and the interactive
'timer is started
'
'When the interactive timer has expired, the timer for background flashing starts.
'To stop the flashing, click the reset button on the form
Private Sub uiUpdateTimer_Tick(sender As Object, e As EventArgs) Handles uiUpdateTimer.Tick
UpdateUI()
If PresentationTimer.IsRunning And IsTimeExpired(PresentationTimer, PresentationMinutes) Then
PresentationTimer.Stop()
InteractiveTimer.Start()
Me.BackColor = Color.Yellow
'add code here for buzzer to show end of presentation time
End If
If InteractiveTimer.IsRunning And IsTimeExpired(InteractiveTimer, InteractiveMinutes) Then
InteractiveTimer.Stop()
BackgroundFlashTimer.Enabled = True
ExtraTimeTimer.Start()
'add code here for buzzer to show end of interactive time
StartBtn.Text = "START"
End If
End Sub
'checks if the timer passed as a parameter is expired by comparing
'the number of elapsed minutes with the number of minutes passed in
'the second parameter
Private Function IsTimeExpired(tmr As Stopwatch, mins As Integer) As Boolean
If tmr.Elapsed.Minutes >= mins Then
Return True
Else
Return False
End If
End Function
'simply updates all the textboxes with the stopwatch values.
Private Sub UpdateUI()
HundredthsTxB.Text = Int(PresentationTimer.Elapsed.Milliseconds / 10).ToString
SecondsTxB.Text = PresentationTimer.Elapsed.Seconds.ToString
MinutesTxB.Text = PresentationTimer.Elapsed.Minutes.ToString
HoursTxB.Text = PresentationTimer.Elapsed.Hours.ToString
OvertimeHundredthsTxB.Text = Int(InteractiveTimer.Elapsed.Milliseconds / 10).ToString
OvertimeSecondsTxB.Text = InteractiveTimer.Elapsed.Seconds.ToString
OvertimeMinutesTxB.Text = InteractiveTimer.Elapsed.Minutes.ToString
OvertimeHoursTxB.Text = InteractiveTimer.Elapsed.Hours.ToString
ExtraTimeHundredthsTxB.Text = Int(ExtraTimeTimer.Elapsed.Milliseconds / 10).ToString
ExtraTimeSecondsTxB.Text = ExtraTimeTimer.Elapsed.Seconds.ToString
ExtraTimeMinutesTxB.Text = ExtraTimeTimer.Elapsed.Minutes.ToString
ExtraTimeHoursTxB.Text = ExtraTimeTimer.Elapsed.Hours.ToString
End Sub
Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
'if either timer is running, stop them both and exit this sub
If PresentationTimer.IsRunning Or InteractiveTimer.IsRunning Then
PresentationTimer.Stop()
InteractiveTimer.Stop()
ExtraTimeTimer.Stop()
StartBtn.Text = "START"
uiUpdateTimer.Enabled = False
Exit Sub
End If
'if the presentation timer hasn't run yet, check if the minutes values are
'valid and start it
If PresentationTimer.ElapsedMilliseconds = 0 Then
PresentationMinutes = CInt(Val(PresTimeTxB.Text))
InteractiveMinutes = CInt(Val(InterTimeTxB.Text))
'if the minutes values are't valid show messagebox and exit this sub
If PresentationMinutes = 0 Or InteractiveMinutes = 0 Then
MessageBox.Show("Please enter valid Presentation and Interactive times.")
Exit Sub
End If
'if minutes values are ok start the presentation timer
uiUpdateTimer.Enabled = True
PresentationTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
'if the presentation timer has been running, but is currently stopped,
'continue the timer and exit this sub
If Not PresentationTimer.IsRunning And PresentationTimer.ElapsedMilliseconds > 0 Then
uiUpdateTimer.Enabled = True
PresentationTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
'if the interactive timer has been running,but is currently stopped,
'continue the timer and exit this sub
If Not InteractiveTimer.IsRunning And InteractiveTimer.ElapsedMilliseconds > 0 Then
uiUpdateTimer.Enabled = True
InteractiveTimer.Start()
StartBtn.Text = "STOP"
Exit Sub
End If
End Sub
Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click
PresentationTimer.Reset()
InteractiveTimer.Reset()
ExtraTimeTimer.Reset()
uiUpdateTimer.Enabled = False
StartBtn.Enabled = True
StartBtn.Text = "START"
UpdateUI()
BackgroundFlashTimer.Enabled = False
Me.BackColor = DefaultBackColor
End Sub
Private Sub BackgroundFlashTimer_Tick(sender As Object, e As EventArgs) Handles BackgroundFlashTimer.Tick
StartBtn.Enabled = False
If Me.BackColor = DefaultBackColor Then
Me.BackColor = Color.Red
Else
Me.BackColor = DefaultBackColor
End If
Me.Update()
End Sub

Threading doesn't complete the task before ending the thread loop

So I did something similar a while ago, but this is essentially a username checker for a (specific) website, they can load in usernames via text file and it will put it into a list box, now I have the start button and it's meant to check each username. Before, however, it froze the program when they checked, but it worked. I tried making it "threaded" so it didn't freeze.
The problem now is that it doesn't check them all, and finishes instantly.
CODE:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
Dim flag As Boolean
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
If goodUsers.ShowDialog() = DialogResult.OK Then
Dim checkerMT As New Thread(
Sub()
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "taken" Then
flag = False
ElseIf cResult = "nottaken" Then
flag = True
End If
If flag = True Then
sb.Append(i & vbNewLine)
Else
Incomplete = Incomplete + 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
End Sub
)
checkerMT.Start()
Try
File.WriteAllText(goodUsers.FileName, sb.ToString)
Catch ex As Exception
Exit Sub
End Try
End If
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
Button6.Enabled = True
End Sub
You cannot access UI elements (UsernameList.Items) from a thread other than the UI. Instead add a background worker to your form to handle the basic threading stuff (progress reporting, finish reporting, exception handling). Pass into this an object that contains the settings your job needs to do its work without interacting with the ui.
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If goodUsers.ShowDialog() = DialogResult.OK Then
'Note: You'll need to add the filenames
BackgroundWorker1.RunWorkerAsync(New State() With {.Names = {}, .FileName = goodUsers.FileName})
End If
End Sub
Class State
Public Names As List(Of String)
Public StringBuilder As New System.Text.StringBuilder
Public Incomplete As Integer
Public Taken As Integer
Public FileName As String
End Class
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim state = CType(e.Argument, State)
For Each i As String In state.Names
Using cli = New System.Net.WebClient()
Dim cResult = cli.DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "nottaken" Then
state.StringBuilder.Append(i & vbNewLine)
Else
state.Incomplete = state.Incomplete + 1
state.Taken = state.Names.Count - state.Incomplete
End If
End Using
Next
IO.File.WriteAllText(state.FileName, state.StringBuilder.ToString)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show(e.Error.ToString(), "Error")
Else
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End If
Button6.Enabled = True
End Sub
The SaveFileDialog can be used as "Using directive"
Why do you create a webrequest on the one hand, and on the other hand use a webclient? That does not make sense at all.
Instead of your strange if condition, write:
flag = (cResult.Equals("nottaken"))
All code you want to run after the actions you are currently running in your thread have to be in your thread as well, because it is async.
You have to invoke if you use user controls within a thread
and so on..
Please turn Option Strict On and Option Infer Off
There are lots of other things you could do better.
Please remove the webrequest and webclient combination yourself, that does not make sense at all.
Take a look at this, I cleared it a little bit:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
Using goodUsers As SaveFileDialog = new SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If not goodUsers.ShowDialog() = DialogResult.OK Then Exit Sub
Dim checkerMT As New Thread(
Sub()
Me.invoke(sub()
Button6.Enabled = False
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If (cResult.toLower().Equals("nottaken")) Then
sb.Append(String.Concat(i , Environment.NewLine)
Else
Incomplete += 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
File.WriteAllText(goodUsers.FileName, sb.ToString)
Button6.Enabled = True
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End Sub)
End Sub
)
checkerMT.IsBackground = True;
checkerMT.Start()
End Sub

Alternate between hiding and showing different controls on form

I'm working on a small project to dynamically add browsers to a form and alternate them on a timer so every tick one browser changes from visible=true to false and so on, but can't for the life of me figure out how to do it.
'Some Variables
Dim Browsers() As WebControl
Dim XMLFile As String = "\\********.xml"
Dim Controllist As New ArrayList
Dim BrowserCount As Integer
Private Sub Left_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set form properties
Me.Location = New Point(Screen.AllScreens(0).Bounds.X, Screen.AllScreens(0).Bounds.Y)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
Me.TopMost = True
Try
'Load xml file with urls
Dim xmlDoc As New XmlDocument
Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("//BottomLeft/url")
xmlDoc.Load(XMLFile)
'Define number of browsers based on xml
Dim numberOfBrowsers As Integer = nodeList.Count + 1
ReDim Browsers(numberOfBrowsers)
'Put urls in an array to use later
Dim UrlList As New ArrayList
Debug.WriteLine("Number of browser is: " & numberOfBrowsers)
For Each inst As XmlNode In nodeList
UrlList.Add(inst.InnerText)
Debug.WriteLine("Added to array: " & inst.InnerText)
Next
'Set properties and add the browsers
For counter As Integer = 0 To numberOfBrowsers - 1
Debug.WriteLine("Start adding browsers")
Browsers(counter) = New WebControl
With Browsers(counter)
Debug.WriteLine("Setting properties")
.Dock = DockStyle.Fill
.Name = "Browers" & counter
.Source = New Uri(UrlList(counter))
If counter = 0 Then
.Visible = True
Else
.Visible = False
End If
End With
'Add the actual browsers after the properties have been set
Me.Controls.Add(Browsers(counter))
AddHandler Browsers(counter).LoadingFrameComplete, AddressOf All_Browsers_Loaded
Debug.WriteLine("Browsers is added. Current Count is: " & counter)
Next
Catch ex As Exception
Debug.WriteLine(ex)
End Try
'Count all the added browsers to alternate between them
Dim allWebBrowser As New List(Of Control)
For Each web As WebControl In FindControlRecursive(allWebBrowser, Me, GetType(WebControl))
Debug.WriteLine("Webcontrols on form: " & web.Name)
BrowserCount = +1
Next
'Start timer to switch browsers
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub All_Browsers_Loaded(ByVal sender As System.Object, ByVal e As Awesomium.Core.FrameEventArgs)
Dim Browser As WebControl = DirectCast(sender, WebControl)
Browser.Zoom = "80"
Debug.WriteLine("Browsers is now zoomed out?")
End Sub
Public Function ChangeBrowser()
Dim Changed As Boolean
For I As Integer = 0 To BrowserCount
With Browsers(I)
If Browsers(I).Visible = True Then
Browsers(I).Visible = False
Changed = True
End If
If Changed = True Then
Browsers(I).Visible = True
Changed = False
End If
If I = BrowserCount Then
Browsers(0).Visible = True
Continue For
End If
End With
Next
Return False
End Function
In the ChangeBrowser() function I'm trying to get the first visible browser and make it not visible. Then set the Changed Boolean to true.
Whats the best way to handle this?

Timer tick when the for loop is completed

How can I tick the timer when the for loop is completed? I don't want to run the timer every X minute, I want to run the timer when the button is clicked and then just tick the timer again when the for loop is completed..
For Each row As DataRow In MTProcessTable.Rows
Try
If checkKeyHelp(process.datain) Then
msg = msgTable.Rows(1)(1).ToString()
MsgBox(msg)
writeMsg("1 MO help at ")
ElseIf checkKeyInfo(process.datain) Then
msg = msgTable.Rows(4)(1).ToString()
MsgBox(msg)
writeMsg("1 MO INFO at ")
Else
MOTable = selectMO(process.mo, process.mt)
moRowNb = MOTable.Rows.Count()
MO = New MO_class
If moRowNb <> 0 Then
MOrow = MOTable.Rows(0)
MO.newuser = MOrow("newuser")
MO.sim_id = MOrow("sim_id")
End If
Catch ex As Exception
logFile("executeTimer ----" & ex.Message)
updateProcessed(process.id, ex.Message)
Finally
updateProcessed(process.id, msg)
End Try
Next row
Private Sub start_btn_Click(sender As System.Object, e As System.EventArgs) Handles start_btn.Click
Timer1.Enabled = True
tm.StartTimer()
End Sub
Based on your comment, it sounds like you want to write a program that will select data and process it. Once that is complete, you want to start the process over by selecting new data and processing that, etc. etc.
If you want to use a timer, I would set it up as follows. It will use a few shared items to start or stop the timer which runs the sub. One problem is that while it is running, the program will be unresponsive. The only time you can 'stop' the program is during the time in between runs. I have the time between threads set to 10 seconds (10000 ms), but you can use any value.
Imports System.Windows.Forms.Timer
Public Class Form1
Private Sub RunProcess()
'Add code to populate datatable
For Each row As DataRow In MTProcessTable.Rows
Try
If checkKeyHelp(Process.datain) Then
msg = msgTable.Rows(1)(1).ToString()
MsgBox(msg)
writeMsg("1 MO help at ")
ElseIf checkKeyInfo(Process.datain) Then
msg = msgTable.Rows(4)(1).ToString()
MsgBox(msg)
writeMsg("1 MO INFO at ")
Else
MOTable = selectMO(Process.mo, Process.mt)
moRowNb = MOTable.Rows.Count()
MO = New MO_class
If moRowNb <> 0 Then
MOrow = MOTable.Rows(0)
MO.newuser = MOrow("newuser")
MO.sim_id = MOrow("sim_id")
End If
End If
Catch ex As Exception
logFile("executeTimer ----" & ex.Message)
updateProcessed(Process.Id, ex.Message)
Finally
updateProcessed(Process.Id, msg)
End Try
Next row
Timer1.Enabled = True
End Sub
Private Timer1 As Timer 'Create timer
Sub Timer1_Tick() 'Handle timer tick
Timer1.Enabled = False
RunProcess()
End Sub
Private blRunning As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1 = New Timer 'Create instance of timer
Timer1.Interval = 10000 'Time in MS before starting next process
AddHandler Timer1.Tick, AddressOf Timer1_Tick 'Add tick handler to timer
Timer1.Enabled = blRunning 'Enable/disable timer
End Sub
Private Sub start_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start_btn.Click
If blRunning = True Then
blRunning = False
start_btn.Text = "Start"
Else
blRunning = True
start_btn.Text = "Stop"
End If
Timer1.Enabled = blRunning
End Sub
End Class