Loop ping and computer status by the network - vb.net

I've got a very basic program which use the IP or Hostname of a computer on the network and ping it in order to know if the computer is online or offline by doing a scan every 10 sec and can be started and stopped by a button, and the loop is making the program freeze.
note: The program is still in development.
Public Class PingerFrm
Dim WorkBool As Boolean
Dim StateStr As String
Dim IPStr As String
Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
WorkBool = True
IPStr = IPTxtBox.Text
Do While WorkBool = True
If My.Computer.Network.Ping(IPStr) Then
StateStr = ("Online")
Else
StateStr = ("Offline")
End If
StateLbl.Text = StateStr
Threading.Thread.Sleep(10000)
Loop
End Sub
Private Sub StopBtn_Click(sender As Object, e As EventArgs) Handles StopBtn.Click
WorkBool = False
End Sub
End Class
Does anyone see what I'm doing wrong or have any ideas ?

The problem is that you have the UI in a busy loop. Take a look at this
Private Async Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
WorkBool = True
IPStr = IPTxtBox.Text
If Net.IPAddress.TryParse(IPStr, Nothing) Then
Dim t As Task
t = Task.Run(Sub()
Dim stateCHGD As Boolean = False
StateStr = ""
Do While WorkBool
If My.Computer.Network.Ping(IPStr) Then
If StateStr <> "Online" Then
stateCHGD = True
StateStr = "Online"
End If
Else
If StateStr <> "Offline" Then
stateCHGD = True
StateStr = "Offline"
End If
End If
If stateCHGD Then
stateCHGD = False
Me.BeginInvoke(Sub()
StateLbl.Text = StateStr
End Sub)
End If
Threading.Thread.Sleep(10000)
Loop
End Sub)
Await t
End If
End Sub

Related

Sort a string item statement

Can I do this shorter from a single timer without needing 5 timers?
I can run it in a sub Timer1.Enabled = True, Timer1.Start () and can change according to preferences, timer2, timer3. so I want to do this to go shorter. I think I should have a case function, or something like that. how could i do it
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("1") Then
BttRnd.PerformClick()
Else
RndTitaniumA2()
End If
End If
Next
Timer1.Stop()
Timer1.Enabled = False
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("2") Then
BttRnd.PerformClick()
Else
RndTitaniumA3()
End If
End If
Next
Timer2.Stop()
Timer2.Enabled = False
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("3") Then
BttRnd.PerformClick()
Else
RndTitaniumA4()
End If
End If
Next
Timer3.Stop()
Timer3.Enabled = False
End Sub
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("4") Then
BttRnd.PerformClick()
Else
RndTitaniumA5()
End If
End If
Next
Timer4.Stop()
Timer4.Enabled = False
End Sub
Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer5.Tick
Dim strx As String
Dim stry As String
For i As Integer = 1 To 10
strx = TextBox1.Lines(i)
stry = TxtBoxIntDrawsY.Text
If stry.Contains(strx) = True Then
Exit For
RndTitaniumA1()
Else
Exit For
If TextBox4.Text = ("5") Then
BttRnd.PerformClick()
Else
MsgBox("")
End If
End If
Next
Timer5.Stop()
Timer5.Enabled = False
End Sub
Let's start with your answers to my questions.
Array indexes in .net start at zero. If you have 10 lines in your textbox, you will get an index out of range exception with For i As Integer = 1 To 10 because there is no index 10. The indexes run from 0 to 9. I still don't see how you are preventing the user from deleting a few lines.
It won't run because you have preceded the code with Exit For. When the code reaches that line it exits the For loop and goes immediately to TimerX.Stop().
Private Sub OpCode()
For i = 0 To 10
Exit For
MessageBox.Show(i.ToString)
Next
End Sub
The MessageBox in the above code will never be displayed. Even if you repositioned the Exit For, you have the exit in both the If block and the Else block so the code would only survive one iteration.
You still didn't answer if the code works as written or why you are using a For loop at all and leaving the loop on the first iteration. Also, the mysterious empty MsgBox and why there are 5 timers.
You can add a break point and step through your code.
Notice the Handles clause in the .Tick event. This event now handles all the Timers.
I used a Select Case as suggested by #preciousbetine in comments. Since a Timer is a component it has no Name property (what I would normally use in the Select Case) so, I used the Is operator. I could also have set the .Tag property for each Timer and used that. I left in the Exit For (repositioned) but be aware; you will only get a single iteration.
Calling an event in code can have unanticipated effects. I showed you how to move the code from the button click to a separate Sub and then call it from the .Click and your routine.
Private Sub AnyTimer_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Timer2.Tick, Timer3.Tick, Timer4.Tick, Timer5.Tick
Dim t As System.Windows.Forms.Timer = DirectCast(sender, System.Windows.Forms.Timer)
Dim strx As String
Dim stry As String
stry = TxtBoxIntDrawsY.Text
For i As Integer = 0 To 9
strx = TextBox1.Lines(i)
If stry.Contains(strx) = True Then
RndTitaniumA1()
Exit For
Else
Dim TextBox4Contents As String = ""
Select Case True
Case t Is Timer1
TextBox4Contents = "1"
Case t Is Timer2
TextBox4Contents = "2"
Case t Is Timer3
TextBox4Contents = "3"
Case t Is Timer4
TextBox4Contents = "4"
Case t Is Timer5
TextBox4Contents = "5"
End Select
If TextBox4.Text = TextBox4Contents Then
RndSub()
Else
Select Case True
Case t Is Timer1
RndTitaniumA2()
Case t Is Timer2
RndTitaniumA3()
Case t Is Timer3
RndTitaniumA4()
Case t Is Timer4
RndTitaniumA5()
Case t Is Timer5
MsgBox("") '???
Case Else
MessageBox.Show("Sender does not match any Timer")
Return
End Select
End If
Exit For
End If
Next
t.Stop()
t.Enabled = False
End Sub
Private Sub BttnRnd_Click(sender As Object, e As EventArgs) Handles BttnRnd.Click
RndSub()
End Sub
Private Sub RndSub()
'Your button code here
End Sub

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.

background worker and threads

i am using a background worker with a timer to call a function that has a for loop... as below :
Private Sub t_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles t.Tick
t.Enabled = False
t.Interval = 5000
Start()
End Sub
Sub Start()
If frm10BckWorker.IsBusy <> True Then
frm10BckWorker.WorkerReportsProgress = True
frm10BckWorker.RunWorkerAsync()
End If
End Sub
Private Sub frm2BckWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles frm10BckWorker.DoWork
StartSending()
End Sub
Sub StartSending()
Try
f.GetNumbers("JK_SP_GET_Users_TO_PROCEED", UsersBulkCnx(8))
count = f.numbersTable.Rows.Count
If count = 0 Then Exit Sub
For I = 0 To count - 1
Id = Trim(f.GetReaderValue(f.numbersTable.Rows(I).Item("ID")))
MO = Trim(f.GetReaderValue(f.numbersTable.Rows(I).Item("MO")))
FamilyName = Trim(f.GetReaderValue(f.numbersTable.Rows(I).Item("FamilyName")))
CName = Trim(f.GetReaderValue(f.numbersTable.Rows(I).Item("ContactNAme")))
ImageUrl = Trim(f.GetReaderValue(f.numbersTable.Rows(I).Item("ImageUrl")))
If Not f.InsertUsers(MO, FamilyName, CName,UsersCnx(8)) Then
PROC = -1
End If
f.DeleteBulk(Id, BulkNbrCnx(8))
f.InsertHistory(MO, FamilyName, CName,ImageUrl, UsersBulkCnx(8))
Next
Catch ex As Exception
f.WriteToText("StartSending", ex.ToString)
End Try
End Sub
what i need is to run in background this function f.InsertHistory that i call in the for loop and keep the loop running at the time without waiting for this function to end to go to the next record in the loop,
any suggestions please?

Trouble With State/Capital Matching Program In VB

Alright, so the program is set up where you have a set of radio buttons with 5 states, and another set of 5 with 5 capitals. There's a button which tells you if they match or not. However I have to do it a certain way where clicking a radio button assigns a variable to both 'strCapital' and 'strChoice' and you compare them to see if they match.
I've tried to figure everything out (since it sounds easy in theory) but I've hit a wall.
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Dim strCapital As String
Dim strChoice As String
Dim strLittleRock As String
Dim strSpringfield As String
Dim strFrankfort As String
Dim strSalem As String
Dim strMadison As String
Private Sub radArkansas_CheckedChanged(sender As Object, e As EventArgs) Handles radArkansas.CheckedChanged
strCapital = strLittleRock
End Sub
Private Sub radIllinois_CheckedChanged(sender As Object, e As EventArgs) Handles radIllinois.CheckedChanged
strCapital = strSpringfield
End Sub
Private Sub radSpringfield_CheckedChanged(sender As Object, e As EventArgs) Handles radSpringfield.CheckedChanged
strChoice = strSpringfield
End Sub
Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
If strCapital = strChoice Then
lblMsg.Text = "Correct"
ElseIf strCapital <> strChoice Then
lblMsg.Text = "Incorrect"
End If
End Sub
Private Sub radLittleRock_CheckedChanged(sender As Object, e As EventArgs) Handles
radLittleRock.CheckedChanged
strChoice = strLittleRock
End Sub
End Class
EDIT: Also I forgot to mention the main problem which is always a bad thing. Basically whenever I run it and enter something incorrect (checking Arkansas and Springfield for example) it always says it's correct.
I did something similar... you can modify this code:
Private Sub btnAmIRight_Click(sender As Object, e As EventArgs) Handles btnAmIRight.Click
' displays if submission is correct
' declare variables
Dim strAmIRight As String
Dim dblMatch As Double
If rbtnAlabama.Checked = True And rbtnMontgomery.Checked = True Then
dblMatch = 1
ElseIf rbtnAlaska.Checked = True And rbtnJuneau.Checked = True Then
dblMatch = 1
ElseIf rbtnArizona.Checked = True And rbtnPhoenix.Checked = True Then
dblMatch = 1
ElseIf rbtnArkansas.Checked = True And rbtnLittleRock.Checked = True Then
dblMatch = 1
ElseIf rbtnCalifornia.Checked = True And rbtnSacramento.Checked = True Then
dblMatch = 1
ElseIf rbtnColorado.Checked = True And rbtnDenver.Checked = True Then
dblMatch = 1
ElseIf rbtnConnecticut.Checked = True And rbtnHartford.Checked = True Then
dblMatch = 1
ElseIf rbtnDelaware.Checked = True And rbtnDover.Checked = True Then
dblMatch = 1
ElseIf rbtnFlorida.Checked = True And rbtnTallahassee.Checked = True Then
dblMatch = 1
ElseIf rbtnGeorgia.Checked = True And rbtnAtlanta.Checked = True Then
dblMatch = 1
Else
dblMatch = 0
End If
' assign code to variable
If dblMatch = 1 Then
strAmIRight = "Correct"
ElseIf dblMatch = 0 Then
strAmIRight = "Try Again"
End If
' display result
lblResult.Text = strAmIRight.ToString
End Sub

Visual Basic: Make 1 button do 2 operations

So I need a button to complete two operations but in two steps. Here is the first buttons code:
First button (button6)
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
p = Process.GetProcessesByName("SbieSvc")
If p.Count > 0 Then
Environment.Exit(0)
Else
End If
Dim antiProcess() As String = {"SbieSvc", "Sandboxiecrypto", "sbiectrl"}
For intI As Integer = 0 To antiProcess.GetUpperBound(0)
For Each x As Process In Process.GetProcessesByName(antiProcess(intI))
x.Kill()
Next
Next
''Sets the Channel''
If TextBox6.Text = "" Then
MsgBox("Please enter a Channelname!", MsgBoxStyle.Information, ("Error"))
GoTo Bottom
End If
Me.Data = Me.TextBox6.Text
Me.Method_1(String.Format("Channel set ({0})", Data))
If (Me.thread0 Is Nothing) Then
Me.thread0 = New Thread(New ThreadStart(AddressOf Method2)) With
{
.IsBackground = True
}
Me.thread0.Start()
End If
''Part Of Grab Urls Method''
Button6.Enabled = False
For i As Integer = 0 To TextBox5.Text Step 1
Dim t1 As New Thread(New ParameterizedThreadStart(Sub() GetUrls(TextBox6.Text)))
t1.Start()
Next
GC.SuppressFinalize(Me)
End Sub
Second button (button1)
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
''start live viewers''
Button1.Enabled = False
For Each itemss In Urls.Items
Dim t1 As New Threading.Thread(Sub() LivePeepz(itemss))
t1.Start()
Next
End Sub
How would I make this code so that button6 will complete it's normal commands, then once done it begins button1's operation. I thought about doing this;
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
p = Process.GetProcessesByName("SbieSvc")
If p.Count > 0 Then
Environment.Exit(0)
Else
End If
Dim antiProcess() As String = {"SbieSvc", "Sandboxiecrypto", "sbiectrl"}
For intI As Integer = 0 To antiProcess.GetUpperBound(0)
For Each x As Process In Process.GetProcessesByName(antiProcess(intI))
x.Kill()
Next
Next
''Sets the Channel''
If TextBox6.Text = "" Then
MsgBox("Please enter a Channelname!", MsgBoxStyle.Information, ("Error"))
GoTo Bottom
End If
Me.Data = Me.TextBox6.Text
Me.Method_1(String.Format("Channel set ({0})", Data))
If (Me.thread0 Is Nothing) Then
Me.thread0 = New Thread(New ThreadStart(AddressOf Method2)) With
{
.IsBackground = True
}
Me.thread0.Start()
End If
''Part Of Grab Urls Method''
Button6.Enabled = False
For i As Integer = 0 To TextBox5.Text Step 1
Dim t1 As New Thread(New ParameterizedThreadStart(Sub() GetUrls(TextBox6.Text)))
t1.Start()
Next
GC.SuppressFinalize(Me)
''start live viewers''
Button1.Enabled = False
For Each itemss In Urls.Items
Dim t1 As New Threading.Thread(Sub() LivePeepz(itemss))
t1.Start()
End Sub
But this doesn't work...Any ideas? Thanks. VB2012
Put your code in seperate functions e.g. Function1 would contain the code you intended for first button click. Function2 would have the code intended for second button click.
Then you have one button with an onClick event code that is
Private Sub Button1_Click(byVal sender as Object, byVal e as EventArgs) Handles Button1.Click
Function1()
Function2()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
FirstOperation()
SecondOperation()
End Sub
Private Sub FirstOperation()
'Button 6 Code
End Sub
Private Sub SecondOperation()
'Button 1 Code
End Sub