Translate

> > C# Thread Handleing in Winform

C# Thread Handleing in Winform

Posted on Tuesday, June 19, 2012 | No Comments

private Thread demoThread = null;

 private void btnBrowse_Click(object sender, EventArgs e)
 {
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                                   this.demoThread = new Thread(new ThreadStart(InsertData));
                                   this.demoThread.Start();
                 // OR
                //ThreadPool.QueueUserWorkItem(new WaitCallback(InsertData), oChalanObject); 
            }
}

// note here InserData is a function which perfrom a insert operation. you can use thread in this two
//way second option is if u want to pass a parameter in Function .


 private void InsertData()
 {
        // write your code here
// note you can not access any winform control directyly if want to access a TextBox or Combobox or //something else then you need a delegate to perform this operation. for example
SetControlPropertyValue(lblProgress, "Text", value);
ClearGroupBoxContent(groupBox1);
}



// Generic Control Value Seter function
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            try
            {
                if (oControl.InvokeRequired)
                {
                    SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                    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())
                        {
                            p.SetValue(oControl, propValue, null);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

// only a specific Control Property set
delegate void GroupBoxPerformStep(GroupBox group1);
        private void ClearGroupBoxContent(GroupBox group1)
        {
            if (group1.InvokeRequired)
            {
                GroupBoxPerformStep del = ClearGroupBoxContent;
                group1.Invoke(del, new object[] { group1 });
                return;
            }
            Result oResult = new Result();
            oResult.ClearAll(group1);
            //myProgressBar.PerformStep();

        }

Leave a Reply

Powered by Blogger.