Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
28/09/2013 06:57:41
 
 
À
27/09/2013 14:58:44
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:
01584387
Vues:
27
>Thanks Viv,
>I think I have it working now.

Just for the heck of it - a Task based version which gets rid of BackgroundWorker altogether and, because it's parallel, should be a whole lot faster:
        private void BtnTask_OnClick(object sender, RoutedEventArgs e)
        {
            btnTask.IsEnabled = btnEmail.IsEnabled = false;
            txtInfo.Text = string.Empty;

            //Dummy list of Primary Keys
            var emailPKs = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            progressBar.Maximum = emailPKs.Count;
            progressBar.Minimum = 0;

            List<Task> tasks = new List<Task>();
            var ui = TaskScheduler.FromCurrentSynchronizationContext();

            foreach (var id in emailPKs)
            {
                var send = Task.Factory.StartNew(() =>
                    {
                    return SendEmail(id);
                });
                tasks.Add(send);

                var display = send.ContinueWith(result =>
                {
                    txtInfo.Text += String.Format("Email to {0} - {1}{2}", send.Result.EmailId,
                                                       send.Result.Status.ToString(), Environment.NewLine);
                    progressBar.Value++ ;
                }, ui)
                ;
            }


            Task.Factory.ContinueWhenAll(tasks.ToArray(), result =>
            {
                txtInfo.Text += "Complete." + Environment.NewLine;
                btnTask.IsEnabled = btnEmail.IsEnabled = true;
                progressBar.Value = progressBar.Maximum;
            }, CancellationToken.None, TaskContinuationOptions.None, ui);

        }
Snag is SMTP (if that's what you are using) may not be able to handle being hit from all directions :-{
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform