Cross thread operation not valid


cross thread operation not valid control accessed from a thread other than

 
Delegate Sub SetControlValueCallback(ByVal oControl As Control, _
ByVal propName As String, _
ByVal propValue As Object)

Private Sub SetControlPropertyValue _
(ByVal oControl As Control, _
ByVal propName As String, _
ByVal propValue As Object)

Try

If oControl.InvokeRequired Then
Dim d As New SetControlValueCallback(AddressOf SetControlPropertyValue)
oControl.Invoke(d, New Object() {oControl, propName, propValue})
Else
Dim t As Type = oControl.[GetType]()
Dim props As Reflection.PropertyInfo() = t.GetProperties()
For Each p As Reflection.PropertyInfo In props
If p.Name.ToUpper() = propName.ToUpper() Then
p.SetValue(oControl, propValue, Nothing)
End If
Next
End If

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, _
ProjectTitle)

End Try


End Sub


use this procedure

Example :

if set text property at another thread

Label.text="Welcome"

exception will come like this

cross thread operation not valid control accessed from a thread other than

we can set text property of label like this


SetControlPropertyValue(Label,"Text", "Welcome")


Comments

Author: Biju26 Aug 2010 Member Level: Gold   Points : 2

Country DA

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Interlace.VKSRETANIUM.ILayer;
using Interlace.VKSRETANIUM.DTO;
using Interlace.Utils;
namespace Interlace.VKSRETANIUM.DataAccess
{
public partial class CountryDA : ICountry
{
Database objDB ;
public CountryDA()
{
if (Interlace.Utils.Utils.ConnectionString==null)
{
Interlace.Utils.Utils.GetConnectionString();
}
objDB = DatabaseFactory.CreateDatabase(Interlace.Utils.Utils.ConnectionString);
}
public int Save(CountryDTO objCountryDTO)
{
int outparam = 0;
try
{
int syncStoreId = Interlace.Utils.Activation.GetStoreId();
using (DbCommand objDbCommand = objDB.GetStoredProcCommand("Save_Country"))
{
objDB.AddInParameter(objDbCommand, "@Id", DbType.Int32, objCountryDTO.Id);
objDB.AddInParameter(objDbCommand, "@Name", DbType.String, objCountryDTO.Name);
objDB.AddInParameter(objDbCommand, "@CreatedBy", DbType.Int32, objCountryDTO.CreatedBy);
if(objCountryDTO.Id != 0)
{
objDB.AddInParameter(objDbCommand, "@CreatedDate", DbType.Int64, objCountryDTO.CreatedDate);
}
else
{
objDB.AddInParameter(objDbCommand, "@CreatedDate", DbType.Int64, Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHHmmss")));
}
objDB.AddInParameter(objDbCommand, "@ModifiedBy", DbType.Int32, objCountryDTO.ModifiedBy);
if(objCountryDTO.Id != 0)
{
objDB.AddInParameter(objDbCommand, "@ModifiedDate", DbType.Int64, Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHHmmss")));
}
else
{
objDB.AddInParameter(objDbCommand, "@ModifiedDate", DbType.Int64, objCountryDTO.ModifiedDate);
}
objDB.AddInParameter(objDbCommand, "@ActiveStatus", DbType.Int16, objCountryDTO.ActiveStatus);
objDB.AddInParameter(objDbCommand, "@StoreId", DbType.Int32, objCountryDTO.StoreId);
objDB.AddInParameter(objDbCommand, "@SyncServerId", DbType.Int32, objCountryDTO.SyncServerId);
objDB.AddInParameter(objDbCommand, "@SyncLogEntry", DbType.Boolean, true);
objDB.AddInParameter(objDbCommand, "@SyncStoreId", DbType.Int32, syncStoreId);
objDB.AddOutParameter(objDbCommand, "@Country_Output", DbType.Int32, 32);
objDB.ExecuteNonQuery(objDbCommand);
outparam = Convert.ToInt32(objDB.GetParameterValue(objDbCommand, "@Country_Output"));
}
}
catch (Exception ex)
{
throw ex;
}
return outparam;
}
public int HardDeleteCountry(CountryDTO ObjCountryDTO)
{
int result = 0;
try
{
using(DbCommand objDbCommand = objDB.GetStoredProcCommand("Delete_Country"))
{
objDB.AddInParameter(objDbCommand, "@Id", DbType.Int32, ObjCountryDTO.Id);
result = objDB.ExecuteNonQuery(objDbCommand);
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public DataTable Search(string strCodn)
{
DataTable dt = new DataTable();
try
{
using(DbCommand objDbCommand = objDB.GetStoredProcCommand("Search_Country"))
{
objDB.AddInParameter(objDbCommand, "@Expression", DbType.String, strCodn );
dt = objDB.ExecuteDataSet(objDbCommand).Tables[0];
}
}
catch (Exception ex)
{
throw ex;
}
return dt;
}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: