mardi 1 décembre 2015

TargetInvocationException in SetValue(Reflection) inside of Thread

I'm getting this exception:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

When I insert extension method 'SetProperty' inside of ThreadStart:

Object temp = element;

PropertyInfo currentProperty = temp.GetType().GetProperty("FontSize");

object currentValue = currentProperty.GetValue(temp);

threads[i] = new Thread(
new ThreadStart(() => 
{ currentProperty.SetValue(temp, Convert.ChangeType(58, currentProperty.PropertyType), null); }));

threads[i].Start();

But when I use SetValue without Threading, everything works without any exceptions or errors.

PropertyInfo currentProperty = temp.GetType().GetProperty("FontSize");

object currentValue = currentProperty.GetValue(temp);

currentProperty.SetValue(temp, Convert.ChangeType(58, currentProperty.PropertyType), null);

Where could be a problem with using Thread? I'm using C# 6, .NET 4.5.6.





Aucun commentaire:

Enregistrer un commentaire