Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Background process updating values in a datagrid
Message
De
01/10/2013 05:51:58
 
 
À
30/09/2013 13:44:45
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:
01584530
Vues:
31
>>>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 :-{
>
>Hmm, interesting stuff Viv. I may make use of it elsewhere for updating a table with 4000 odd updates, Unless that might be a problem too.

If you mean updating an ADO.NET Datatable then I don't think it would work - Datatable is threadsafe for reads but not for writes.

One addition to .NET 4.5 (if you are using it) is the BindingOperations.EnableCollectionSyncronization() method. If you have a collection databound to the UI this will enable you to update it on a different thread with the UI automatically updated. See: http://dotnet.dzone.com/articles/wpf-45-%E2%80%93-part-7-accessing for a simple example. Since you originally wanted to update a DataGrid the approach might well be worth exploring....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform