Hi,
I am developing a system with c#.net. I have a simple application that create an excel book and write data which were retrieved from sql data base and save in hard disk. I want to show the prgress of data writing using a label. I use a thread function to display the progress with a lalbel. when i click the button, the thread is running but it the value is not shown in the label. but when the excel book creting function is completed then a value is displayed in the lebel. wht is wrong ?
here is the thread function...
private void DataProgress(){
int value = 0; while (true){
value++;
SetControlPropertyValue(label1, "Text", value.ToString()); Thread.Sleep(100);}
}
// end
private
void btnStart_Click(object sender, EventArgs e){
Thread
trd = new Thread(new ThreadStart(this.DataProgress));trd.IsBackground =
true;trd.Start();
GenerateReport();
trd.Suspend();
MessageBox.Show("end"); } delegate void SetControlValueCallback(Control oControl, string propName, object propValue); //Leo Delegate private void SetControlPropertyValue(Control oControl, string propName, object propValue){
if (oControl.InvokeRequired){
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);oControl.Invoke(d,
new object[] { oControl, propName, propValue });}
else{
Type t = oControl.GetType();System.Reflection.
PropertyInfo[] props = t.GetProperties(); foreach (System.Reflection.PropertyInfo p in props){
if (p.Name.ToUpper() == propName.ToUpper()){
p.SetValue(oControl, propValue,
null);}
}
}
}
Nilanka DharmadasaPosted Aug 22, 2008, 8:22 AM
In ur application, eventhough you use multithreading you don't get the benefits of it. The main thread is busy with ';GenerteReports' method and also UI update calls. Here, the second thread does nothing but just call the 'SetControlPropertyValue' method. Inside that method again the main thread calls the control access methods which will actually do the UI update.
You should call 'generate reports' in a separate thread.
Hope you got my point.