Hola a todos.
No se si alguien ha probado el nuevo entorno de Visual C# Express, la verdad
es que esta muy bien en cuanto a funcionalidad para el desarrollador.
Bueno el tema es que estoy realizando una aplicacion en la cual utilizo un
thread y cuando llamo a este thread, al entrar en el llamo a un control LABEL
para actualizar un mensaje de estado. Bien cuando llega a ese punto me
muestra una excepción la cual no entiendo.
No se si alguien me puede ayudar?, gracias.
Este es el ejemplo:
public Form1()
{
InitializeComponent();
this.lbUsr.Text = "Inicio...";
lpth = new Thread(new ThreadStart(RestoreUsr));
lpth.Start();
}
private void RestoreUsr()
{
try
{
CInterBase lpDb = new CInterBase();
lpDb.SetConnect("Demo", "usr", "psw");
this.lbUsr.Text = "Conexión Ok.";
if (lpDb.get_Connection.State ==
System.Data.ConnectionState.Open)
{
OdbcCommand lpCmd = lpDb.get_Connection.CreateCommand();
lpCmd.CommandText = "UPDATE TABLE";
lpCmd.ExecuteNonQuery();
}
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show("ERROR->" + exc.Message);
}
finally
{
lpth = null;
Application.Exit();
}
{System.InvalidOperationException: Cross-thread operation not valid: Control
'lbUsr' accessed from a thread other than the thread it was created on.
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.Label.set_Text(String value)
at ResotoreUsrEpk.Form1.RestoreUsr() in C:\Programacion\C#\Proyectos
Mabyc\ResotoreUsrEpk\ResotoreUsrEpk\Form1.cs:line 32}
Leer las respuestas