Showing posts with label My Solution. Show all posts
Showing posts with label My Solution. Show all posts

Sunday, December 27, 2009

My Solution: Cross-thread operation not valid error while setting a value to control through property


Last week I was working on a code in which a control's property was being accessed from a thread. Obviously as it was a multithreaded application, it should throw the "Cross-thread operation not valid" error. I know that if we are accessing any method from a different thread, we need to use the delegates and events. But my concern was how I can do the same with property as there are no delegates associated with properties of a control.

I created one simple VS 2008 application which set the text property of a text box on a different thread, when I ran the program, I got the following exception.

"Cross-thread operation not valid: Control 'textBox' accessed from a thread other than the thread it was created on."

To tackle this I was thinking about a way which is similar to the techniques we use while handling the same error for methods i.e. make use of events and delegates.

So I created a simple delegate and event as follows

private delegate void SetControlValue(Control controlType,
string propertyName, object value);
private event SetControlValue SetControlValueEvent = null;


And wrote a new method as follows

private void SetControlValueMethod(Control controlType,
string propertyName, object value)

{ 
if (this.InvokeRequired)
{
this.Invoke(new SetControlValue(SetControlValueMethod),new object[]   

{controlType, propertyName, value });
}
else 

{
        PropertyInfo[] props = controlType.GetType().GetProperties(); 

foreach (PropertyInfo pi in props)
{
    if (pi.Name.ToUpper().Trim() == propertyName.ToUpper().Trim())
       {
            pi.SetValue(controlType, value, null);
       }
}
}
}


Here I have used reflection to get the PropertyInfo and used the Setvalue method to assign the appropriate value to the control.

And I used instead of calling

textBox.Text = _number.ToString();

I called the new method to set the values

SetControlValueMethod(textBox, "Text", _number.ToString());


This need to modify to add intellisense to the method so that the control type and their respective properties should come automatically and any error that may cause due to typo can be avoided.

Friday, May 18, 2007

My Solution for "An error has occurred on the computer running IIS. Try restarting the IIS server" Error

I got this error while I was working on SQL Server mobile and Merge Replication. The solution was working fine initially but later on I switched my emulator from WM 5 to WM 6 & I got this error. I tried the same solution back on WM 5 , but it gave me same error, I tried to solved it by providing network credentials but it didn't solved my problem. So used my (or I'll say everyone's ) most effective weapon to solve ambiguous errors i.e. System reset :). I restart my machine & the solution start working fine. Magic!!!
I donno reason behind this, but it served my purpose.