14/04/2013, 05:57
|
| | Fecha de Ingreso: julio-2012 Ubicación: panama
Mensajes: 19
Antigüedad: 12 años, 4 meses Puntos: 0 | |
Respuesta: Ejecutar Sub menu cada 5 minutos Analizando un poco se me viene que la cuestión no está en la aplicación auto realice un ping cada 5 minutos para verificar el status de las ip, mas bien deberia actualizar el resultado cada 5 minutos automatizado. ese es el detalle adjunto el codigo completo para ver cual es su valiosa opinión.
Imports System.Data.OleDb
Imports System.Drawing
Public Class prueba
Private Counter As Integer = 0
Private ConnString As New OleDbConnectionStringBuilder("Provider=Microsoft.J et.OLEDB.4.0;Data Source=C:\test\pingDB.mdb;User Id=admin;Password=;")
Private Sub PingToolStripMenuItem_load(sender As System.Object, ByVal e As System.EventArgs) Handles PingToolStripMenuItem.Click
Timer.Interval = 10000
Timer.Enabled = True
AddHandler Timer.Tick, AddressOf Timer_Tick
Timer.Start()
Counter = 0
PingToolStripMenuItem.Enabled = True
CargarToolStripMenuItem.Enabled = False
Dim NetworkSwitchList As List(Of NetworkSwitch) = GetSwitchListFromDGV()
Threading.ThreadPool.SetMaxThreads(30, 400)
For Each switchobject As NetworkSwitch In NetworkSwitchList
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf DoPing), switchobject)
Next
End Sub
Private Sub Timer_Tick(sender As Object, ByVal e As EventArgs)
Timer.Start()
End Sub
Private Sub DoPing(ByVal SwitchObj As Object)
Dim Switch As NetworkSwitch = DirectCast(SwitchObj, NetworkSwitch)
Dim Result As Net.NetworkInformation.PingReply
Dim SendPing As New Net.NetworkInformation.Ping
Try
Result = SendPing.Send(Switch.IPAddress)
If Result.Status = Net.NetworkInformation.IPStatus.Success Then
Switch.Result = True
Else
Switch.Result = False
End If
Catch ex As Exception
Switch.Result = False
End Try
Switch.LastTested = Now
UpdateDatabase(Switch)
Counter += 1
Me.Invoke(New Action(Of NetworkSwitch)(AddressOf UI_PingComplete), Switch)
End Sub
Private Sub UI_PingComplete(ByVal sw As NetworkSwitch)
UpdateDGV(sw)
logbox.AppendText(vbNewLine & sw.IPAddress & " Prueba Completa " & sw.Result & vbNewLine)
If Counter = PingDGV.RowCount Then
logbox.AppendText(" Analisis Culminado " & vbNewLine)
PingToolStripMenuItem.Enabled = True
CargarToolStripMenuItem.Enabled = True
Else : logbox.BackColor = Color.Black
logbox.ForeColor = Color.GreenYellow
End If
End Sub
Private Sub UpdateDGV(ByVal switch As NetworkSwitch)
For Each row As DataGridViewRow In PingDGV.Rows
If CInt(row.Cells("IDcol").Value) = switch.ID AndAlso CStr(row.Cells("IPcol").Value) = switch.IPAddress Then
row.Cells("Resultcol").Value = switch.Result
row.Cells("LastTestedcol").Value = switch.LastTested
If switch.Result = True Then
row.Cells("Resultcol").Style.BackColor = Color.GreenYellow
Else
row.Cells("Resultcol").Style.BackColor = Color.Red
End If
End If
Next
End Sub
Private Function GetSwitchListFromDGV() As List(Of NetworkSwitch)
Dim switchlist As New List(Of NetworkSwitch)
For Each row As DataGridViewRow In PingDGV.Rows
Dim switch As New NetworkSwitch
With switch
.ID = CInt(row.Cells("IDcol").Value)
.IPAddress = CStr(row.Cells("IPcol").Value)
.LastTested = CDate(row.Cells("LastTestedcol").Value)
.Result = CBool(row.Cells("Resultcol").Value)
.Departamento = CStr(row.Cells("Departamentocol").Value)
.Serie = CStr(row.Cells("Seriecol").Value)
End With
switchlist.Add(switch)
Next
Return switchlist
End Function
Private Function GetSwitchListFromDatabase() As List(Of NetworkSwitch)
'Create our connection that will be used to connect to the Access database
Dim AccessConnection As New OleDbConnection(ConnString.ConnectionString)
Dim SelectCommand As New OleDbCommand("SELECT * FROM Albr ORDER BY IP ASC", AccessConnection)
Dim PingList As New List(Of NetworkSwitch)
AccessConnection.Open()
Dim Reader As OleDbDataReader = SelectCommand.ExecuteReader
Do While Reader.Read
Dim CurrentSwitch As New NetworkSwitch
With CurrentSwitch
.ID = CInt(Reader("ID"))
.IPAddress = CStr(Reader("IP"))
.Departamento = CStr(Reader("Departamento"))
.Serie = CStr(Reader("Serie"))
If IsDBNull(Reader("LastTested")) Then
.LastTested = Nothing
Else
.LastTested = CDate(Reader("LastTested"))
End If
.Result = CBool(Reader("Result"))
End With
PingList.Add(CurrentSwitch)
Loop
AccessConnection.Close()
Return PingList
End Function
Private Sub UpdateDatabase(ByVal sw As NetworkSwitch)
Dim AccessConnection As New OleDbConnection(ConnString.ConnectionString)
Dim UpdateCommand As New OleDbCommand("UPDATE Albr SET Result=@Result, LastTested=@CurrentTime WHERE ID=@ID", AccessConnection)
With UpdateCommand.Parameters
.Add("@Result", OleDbType.Boolean).Value = sw.Result
.Add("@CurrentTime", OleDbType.Date).Value = sw.LastTested
.Add("@ID", OleDbType.Integer).Value = sw.ID
End With
AccessConnection.Open()
UpdateCommand.ExecuteNonQuery()
AccessConnection.Close()
End Sub
Private Sub LoadDGV()
PingDGV.Rows.Clear()
Dim SwitchList As List(Of NetworkSwitch) = GetSwitchListFromDatabase()
For i As Integer = 0 To SwitchList.Count - 1
PingDGV.Rows.Add()
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IDcol").Value = SwitchList(i).ID
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IPcol").Value = SwitchList(i).IPAddress
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Resultcol").Value = SwitchList(i).Result
PingDGV.Rows(PingDGV.RowCount - 1).Cells("LastTestedcol").Value = SwitchList(i).LastTested
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Departamentocol").Value = SwitchList(i).Departamento
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Seriecol").Value = SwitchList(i).Serie
If SwitchList(i).Result = True Then
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Resultcol").Style.BackColor = Color.BurlyWood
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IDcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IPcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("LastTestedcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Departamentocol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Seriecol").Style.BackColor = Color.Beige
Else
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Resultcol").Style.BackColor = Color.BurlyWood
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IDcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("IPcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("LastTestedcol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Departamentocol").Style.BackColor = Color.Beige
PingDGV.Rows(PingDGV.RowCount - 1).Cells("Seriecol").Style.BackColor = Color.Beige
End If
Next
End Sub
Private Sub CargarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CargarToolStripMenuItem.Click
LoadDGV()
End Sub
Gracias a todos por su oportuna colaboracion |