Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
26/09/2013 04:57:11
 
 
À
25/09/2013 15:07:15
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:
01584173
Vues:
36
>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);
            }
        }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform