see my code
publicpartialclassForm1:Form{BackgroundWorker m_oWorker;publicForm1(){InitializeComponent();
m_oWorker =newBackgroundWorker();// Create a background worker thread that ReportsProgress &// SupportsCancellation// Hook up the appropriate events.
m_oWorker.DoWork+=newDoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.ProgressChanged+=newProgressChangedEventHandler(m_oWorker_ProgressChanged);
m_oWorker.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
m_oWorker.WorkerReportsProgress=true;
m_oWorker.WorkerSupportsCancellation=true;}privatevoid button1_Click(object sender,EventArgs e){
m_oWorker.RunWorkerAsync();}List<Employee> oEmp =newList<Employee>();void m_oWorker_DoWork(object sender,DoWorkEventArgs e){// The sender is the BackgroundWorker object we need it to// report progress and check for cancellation.//NOTE : Never play with the UI thread here...for(int i =0; i <100; i++){Thread.Sleep(100);// Periodically report progress to the main thread so that it can// update the UI. In most cases you'll just need to send an// integer that will update a ProgressBar
oEmp.Add(newEmployee(1,"Dipak"));
m_oWorker.ReportProgress(i,newobject[]{ oEmp });// Periodically check if a cancellation request is pending.// If the user clicks cancel the line// m_AsyncWorker.CancelAsync(); if ran above. This// sets the CancellationPending to true.// You must check this flag in here and react to it.// We react to it by setting e.Cancel to true and leavingif(m_oWorker.CancellationPending){// Set the e.Cancel flag so that the WorkerCompleted event// knows that the process was cancelled.
e.Cancel=true;
m_oWorker.ReportProgress(0);return;}}//Report 100% completion on operation completed
m_oWorker.ReportProgress(100);}void m_oWorker_ProgressChanged(object sender,ProgressChangedEventArgs e){List<Employee> oEmp =(List<Employee>)e.UserState;
lblStatus.Text="Processing......"+ e.ProgressPercentage.ToString()+"%";}void m_oWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e){// The background process is complete. We need to inspect// our response to see if an error occurred, a cancel was// requested or if we completed successfully. if(e.Cancelled){
lblStatus.Text="Task Cancelled.";}// Check to see if an error occurred in the background process.elseif(e.Error!=null){
lblStatus.Text="Error while performing background operation.";}else{// Everything completed normally.
lblStatus.Text="Task Completed...";}}}publicclassEmployee{int ID =0;stringName="";publicEmployee(){}publicEmployee(int ID,stringName){this.ID = ID;this.Name=Name;}}
i am passing custom data to ProgressChanged event
like m_oWorker.ReportProgress(i, new object[] { oEmp });
please show me how to read back it from ProgressChanged event
.
i tried this but fail.
void m_oWorker_ProgressChanged(object sender,ProgressChangedEventArgs e){List<Employee> oEmp =(List<Employee>)e.UserState;
lblStatus.Text="Processing......"+ e.ProgressPercentage.ToString()+"%";}