private void btnEmail_Click(object sender, RoutedEventArgs e) > { > > this.BackgroundWorker = new BackgroundWorker(); > this.BackgroundWorker.WorkerReportsProgress = true; > this.BackgroundWorker.DoWork += new DoWorkEventHandler(this.EmailQueueBackgroundWorker_DoWork); > this.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(this.BackgroundWorker_ProgressChanged); > > this.progressBar.Maximum = 100; > this.progressBar.Minimum = 0; > > //// Start the Asynch Worker > this.BackgroundWorker.RunWorkerAsync(); > }>
private void EmailQueueBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) > { > // Create the process object in worker thread > EmailQueueProcess emailQueue = new EmailQueueProcess(); > > // register the handler > emailQueue.ProcessStateChange += new ProcessStateChangeEventHandler(SendEmail_ProcessStateChange); > > // build the list of Primary Keys to process > DataTable emailQueuePKs = this.BuildEmailQueuePKsDataTable(); > > // Kick off the process > emailQueue.ProcessQueue(emailQueuePKs); > }>
> /// <summary> > /// State change handler for the business process > /// </summary> > /// <param name="sender"></param> > /// <param name="e"></param> > private void SendEmail_ProcessStateChange(object sender, ProcessStateChangeEventArgs e) > { > if (e.ProcessState == ProcessState.ProgressChanged) > { > this.BackgroundWorker.ReportProgress(e.ProgressPercentage, e); > } > > } > > /// <summary> > /// progress changed handler for the asynch call back of the backround worker > /// </summary> > /// <param name="sender"></param> > /// <param name="e"></param> > private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) > { > ProcessStateChangeEventArgs businessStateArgs = e.UserState as ProcessStateChangeEventArgs; > if (businessStateArgs != null) > { > // Check e.ProcessState enum and the percentage here and set the status bar appropriately > if (businessStateArgs.ProcessState == ProcessState.ProgressChanged) > { > this.progressBar.Value = e.ProgressPercentage; > > // can I update the datatable being displayed in the grid? > > } > } > }>
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { string s = e.UserState as string; textBox.Text = "Sent to " + s; } private void EmailQueueBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; List<string> recipients = new List<string>{"Fred","Joe","Phil"}; foreach (var recipient in recipients) { worker.ReportProgress(1, recipient ); System.Threading.Thread.Sleep(5000); } }