Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
27/09/2013 08:48:52
 
 
À
26/09/2013 04:57:11
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
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:
01584318
Vues:
20
>>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?
>
>Not sure I understand the problem but the ProgressChangedEventHandler has access to the UI and you can pass any info back to it in ProgressChangedEventArgs.UserState. Simple example:
       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);
>            }
>        }
Viv,

thanks. I'm not sure I understand the problem either :)

My original code was passing off a list of Primary keys to be processed and then the business process was processing those records and I was trying to update the UI whenever each record was processed.

I've changed my code closer to the example you gave so that the looping through the list of primary keys occurs in the UI code and just the actual sending of the email gets sent to the business process. This way I am able to update the data in the grid as a record gets processed. I'm still not terribly happy with what I've done but I'll spend some more time on it before I come back again.
Frank.

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

Click here to load this message in the networking platform