Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
26/09/2013 12:42:33
John Baird
Coatesville, Pennsylvanie, États-Unis
 
 
À
26/09/2013 11:26:59
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:
01584242
Vues:
24
>>>Sorry Craig,
>>>
>>>I forgot to say (and to choose the correct application type when I created the message) that this is a WPF desktop application. That tool looks like it's for the web.
>>>
>>>Or do I understand the write up on it incorrectly?
>>>
>>>
>>>>Look at SignalR. It may do what you want.
>>>>
>>>>>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?
>>
>>
>>if I remember correctly, The backgroundworker thread can talk to the UI thread, but a regular thread has to be marshaled to the UI thread. If you're using MVVMlight or one of the MVVM frameworks, there are functions to do this.
>
>Usually you need to go through a dispatcher to update the UI thread: http://msdn.microsoft.com/en-us/library/ms741870.aspx.

Yep and MVVM light does this for you ..
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform