Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
25/09/2013 15:07:15
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Background process updating values in a datagrid
Versions des environnements
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01584125
Message ID:
01584125
Vues:
56
Hi,

please bear with me as I am not sure of what I am doing. :)

I have a datagrid showing some data. A background process loops through the list of items in the grid and I want to update the data shown in the grid with the result of the process as each item is dealt with.

Specifically my grid shows a list of emails to be sent. When an email gets sent I save the time it was sent and if there was an error during the send gets saved to the email record as well. So I want this to be reflected in the grid.

This is the code that starts off the background process
        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();
        }
This code calls the process to go through the list after first building up a list of Primary Keys of the emails to be sent:
        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); 
       }
The supporting code for updating the progress bar:
        /// <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?

                }
            }
        }
Is there a way for me to hook into this code to update the datagrid to display the updated values?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform