Estoy probando un simple ejemplo con 3 subprocesos que se ejecutan a la par... en uno de ellos quiero modificar un datagridview (exactamente en el segundo hilo) y los cuales se ejecutan infinitamente...
heee aquí mi codigo::
Código:
Imports System.Windows.Forms.DataGridView
Imports System.Threading
Public Class Form1
Private trd As Thread
Private trd2 As Thread
Private trd3 As Thread
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MsgBox("Este es el Hilo principal", MsgBoxStyle.Information, "Informate")
End Sub
Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
If Me.trd.IsAlive Then
Me.trd.Abort()
End If
If Me.trd2.IsAlive Then
Me.trd2.Abort()
End If
If Me.trd3.IsAlive Then
Me.trd3.Abort()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
trd2 = New Thread(AddressOf ThreadTask2)
trd2.IsBackground = True
trd2.Start()
trd3 = New Thread(AddressOf ThreadTask3)
trd3.IsBackground = True
trd3.Start()
End Sub
Private Sub ThreadTask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = ProgressBar1.Step * rnd.Next(-1, 2)
newval = ProgressBar1.Value + stp
If newval > ProgressBar1.Maximum Then
newval = ProgressBar1.Maximum
ElseIf newval < ProgressBar1.Minimum Then
newval = ProgressBar1.Minimum
End If
'Control.CheckForIllegalCrossThreadCalls = False
Me.TextBox1.Text = newval
ProgressBar1.Value = newval
Thread.Sleep(150)
Loop
End Sub
Private Sub ThreadTask2()
Try
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = ProgressBar2.Step * rnd.Next(-1, 2)
newval = ProgressBar2.Value + stp
If newval > ProgressBar2.Maximum Then
newval = ProgressBar2.Maximum
ElseIf newval < ProgressBar2.Minimum Then
newval = ProgressBar2.Minimum
End If
'Control.CheckForIllegalCrossThreadCalls = False
Me.TextBox2.Text = newval
ProgressBar2.Value = newval
'CrearDatagrid(newval)
Thread.Sleep(10000)
Loop
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ThreadTask3()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = ProgressBar3.Step * rnd.Next(-1, 2)
newval = ProgressBar3.Value + stp
If newval > ProgressBar3.Maximum Then
newval = ProgressBar3.Maximum
ElseIf newval < ProgressBar3.Minimum Then
newval = ProgressBar3.Minimum
End If
'Control.CheckForIllegalCrossThreadCalls = False
Me.TextBox3.Text = newval
ProgressBar3.Value = newval
Thread.Sleep(50)
Loop
End Sub
Function CrearDatagrid(ByVal NumeroFila As Integer) As Integer
Try
If NumeroFila = 0 Then
NumeroFila = 5
End If
'Me.DataGridView1 = New DataGridView
Me.DataGridView1.Rows.Clear()
Me.DataGridView1.Columns.Clear()
For i As Integer = 0 To 3
Me.DataGridView1.Columns.Add("columna" + CStr(i), "Columna: " + CStr(i + 1))
Next
For i As Integer = 1 To NumeroFila
Me.DataGridView1.Rows.Add("Fila", i, "Ejemplo", ".... ")
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Sub DataGridView1_DataError(sender As System.Object, _
e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError
''evitando Errores
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
trd2 = New Thread(AddressOf ThreadTask2)
trd2.IsBackground = True
trd2.Start()
End Sub
End Class
Todo esta en un ciclo infinito
bueno .. después de varias pasadas me vota el error
"No se controló System.NullReferenceException"
"Referencia a objeto no establecida como instancia de un objeto."
estoy seguro que es cuando manipulo el datagrid, ya que he quitado la llamada a la funcion y se ejecuta correctamente hasta que termino la ejecución!
les agradezco de antemano y espero me puedan ayudar ... GRACIAS!