Visual Basic.NET - Problema con thread

 
Vista:
sin imagen de perfil

Problema con thread

Publicado por Octavio (2 intervenciones) el 28/05/2010 22:35:19
Tengo el codigo que les dejo abajo. Y lo que quiero hacer es que mientras se van realizando distintas consultas a la base de datos se valla cargando una barra para informar al usuario cuanto falta para terminar de cargar el sistema.

Resulta que no se como hacer e thread. Ya que si ejecuto en el _Load oHilo.Start() me da error en la linea:

barraPor.Value = 5

el error es: InvalidOperationException was unhandled
Cross-thread operation not valid: Control 'barraPor' accessed from a thread other than the thread it was created on.

Si entiendo bien quiere decir que otro treas maneja esa accion por lo cual se superponen las tareas o algo por el estilo

Espero me puedan ayuda a hacer una ventana de splash de este formulario. Desde ya muchas gracias.

El codigo es:

Option Explicit On

Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.Threading

Public Class Splash

Delegate Sub Reinvocar(ByVal iPorc As Integer)
Dim oHilo As New Threading.Thread(AddressOf cargaDeDatos)

Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'oHilo.Start()
cargaDeDatos()
End Sub

Private Sub cargaDeDatos()
Dim porc As Integer = 0
Dim total As Integer = 0
Dim Conecta As String = "server=localhost;uid=root;password=;database=sueldos"
Dim Conecta_on As New MySqlConnection(Conecta)
Conecta_on.Open()
Dim strSql As String = "SELECT count(cuit) as cant FROM relacionee r"
Dim CmSql As MySqlCommand = New MySqlCommand(strSql, Conecta_on)
Dim drSql As MySqlDataReader
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
If drSql.Read() Then
total = total + Integer.Parse(drSql.Item("cant").ToString())
End If
End If
Conecta_on.Close()
Console.WriteLine("TOTAL1 = " & total)
Conecta_on.Open()
strSql = "SELECT count(cuit) as cant FROM empleado e"
CmSql = New MySqlCommand(strSql, Conecta_on)
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
If drSql.Read() Then
total = total + Integer.Parse(drSql.Item("cant").ToString())
End If
End If
Conecta_on.Close()
Console.WriteLine("TOTAL2 = " & total)
Conecta_on.Open()
strSql = "SELECT count(cuil) as cant FROM empleador e"
CmSql = New MySqlCommand(strSql, Conecta_on)
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
If drSql.Read() Then
total = total + Integer.Parse(drSql.Item("cant").ToString())
End If
End If
Conecta_on.Close()
Console.WriteLine("TOTAL3 = " & total)
barraPor.Value = 5
Conecta_on.Open()
lblTabla.Text = "EMPLEADORES"
strSql = "SELECT * FROM empleador e"
CmSql = New MySqlCommand(strSql, Conecta_on)
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
Do While drSql.Read()
lblActual.Text = drSql.Item("apeNom").ToString()
porc = porc + 1
barraPor.Value = (porc * 100) / total
lblPorc.Text = (porc * 100) / total & " %"
Loop
End If
Conecta_on.Close()
Conecta_on.Open()
strSql = "SELECT * FROM empleado e"
CmSql = New MySqlCommand(strSql, Conecta_on)
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
Do While drSql.Read()
lblActual.Text = drSql.Item("apeNom").ToString()
porc = porc + 1
barraPor.Value = (porc * 100) / total
lblPorc.Text = (porc * 100) / total & " %"
Loop
End If
Conecta_on.Close()
Conecta_on.Open()
strSql = "SELECT * FROM relacionee e"
CmSql = New MySqlCommand(strSql, Conecta_on)
drSql = CmSql.ExecuteReader()
If drSql.HasRows() Then
Do While drSql.Read()
lblActual.Text = drSql.Item("cuil").ToString()
porc = porc + 1
barraPor.Value = (porc * 100) / total
lblPorc.Text = (porc * 100) / total & " %"
Loop
End If
Conecta_on.Close()
Me.SendToBack()
Inicio.Show()
Me.SendToBack()
End Sub
End Class
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder