Friday, October 14, 2011

In windows application, how to access control running in one thread from another thread?


Function Call:
=============

SetControlPropertyValue(lblFail, "Text", "File doesn't exist");
SetControlPropertyValue(lblFail, "Visible", "true");
               

Function definition:
====================
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
       private void SetControlPropertyValue(Control oControl, string propName, object propValue)
       {
           
           if (oControl.InvokeRequired)
           {
               SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
               if (option == 1) //  enable property
               {
                  oControl.Invoke(d, new object[] { oControl, propName, Convert.ToBoolean(propValue) });
               }
             
               else
               {
                   oControl.Invoke(d, new object[] { oControl, propName, propValue });
               }
           }
           else
           {
               Type t = oControl.GetType();
               PropertyInfo[] props = t.GetProperties();
               foreach (PropertyInfo p in props)
               {
                   if (p.Name.ToUpper() == propName.ToUpper())
                   {
                       if (option == 1) //  enable property
                       {
                           p.SetValue(oControl, Convert.ToBoolean(propValue), null);
                      }
                       
                       else
                       {

                           p.SetValue(oControl, propValue, null);
                       }
                       
                   }
                   
               }
           }
       }

No comments:

Post a Comment