Hi,
I have just upgraded to .NET 4.0
One of the good things that .NET has is Task Parallel Library (TPL). It is supposed to replace ThreadPool that appeared in previous version of .NET. One of the problems though that I experience is that I cannot catch exceptions within the code that is executed using TPL.
Is there anyone who can give me more information about TPL exceptions?
Thanks in advance
Loading

VulpesRank #55 · 29.2K reputationPosted May 3, 2011, 10:28 AM EDT
Antonios Dev37 reputationPosted May 6, 2011, 8:53 AM EDT
I enclosed the result of the Task into Try/Catch which catches AggregateException and Flattens it.
Antonios Dev37 reputationPosted May 6, 2011, 2:48 PM EDT
Can you help me please
private void InserClient(int ClientId)
{
Task tsk;
tsk = Task.Factory.StartNew(() =>
{
AddClient(ClientId)
});
try
{
tsk.ContinueWith(x =>
{
LoadClients();
EnableButtons(true);
});
}
catch (AggregateException ex)
{
EnableButtons(true);
throw ex.Flatten();
}
}
private void AddClient(int ClientId)
{
using (CLIENTSDataContext dataContext = new CLIENTSDataContext(MyConnectionString))
{
Convert.ToInt32("S");
dataContext.INSERT_Client(ClientName, ClientAddress);
}
}
Sam HobbsRank #107 · 20.5K reputationPosted May 6, 2011, 3:05 PM EDT
Antonios Dev37 reputationPosted May 7, 2011, 12:38 AM EDT
catch (Exception ex)
{
EnableButtons(true);
throw ex.Flatten();
}
VulpesRank #55 · 29.2K reputationPosted May 7, 2011, 5:28 AM EDT
Antonios Dev37 reputationPosted May 7, 2011, 6:10 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2011, 10:53 AM EDT
private void InserClient(int ClientId)
{
Task tsk;
tsk = Task.Factory.StartNew(() =>
{
AddClient(ClientId)
});
try
{
tsk.ContinueWith(x =>
{
LoadClients();
EnableButtons(true);
});
tsk.Wait();
}
catch (AggregateException ex)
{
EnableButtons(true);
throw ex.Flatten();
}
}
Antonios Dev37 reputationPosted May 7, 2011, 2:15 PM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2011, 2:56 PM EDT
Antonios Dev37 reputationPosted May 8, 2011, 12:31 AM EDT
This thing started to confuse me. TPL was supposed to be much easier to use that traditional Threading yet is so unclear and confusing.
VulpesRank #55 · 29.2K reputationPosted May 8, 2011, 2:39 AM EDT
Antonios Dev37 reputationPosted May 8, 2011, 3:13 AM EDT
Antonios Dev37 reputationPosted May 8, 2011, 3:30 AM EDT
Thanks
VulpesRank #55 · 29.2K reputationPosted May 8, 2011, 4:06 AM EDT
If you declare this field in your Form class:
private TaskScheduler uiScheduler;
then in the constructor initialize it as follows:
public Form1()
{
InitializeComponent();
uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
and then change InserClient as follows:
private void InserClient(int ClientId)
{
Task tsk;
tsk = Task.Factory.StartNew(() =>
{
AddClient(ClientId)
});
try
{
tsk.ContinueWith(x =>
{
LoadClients();
EnableButtons(true);
}, uiScheduler);
tsk.Wait();
}
catch (AggregateException ex)
{
EnableButtons(true);
throw ex.Flatten();
}
}
it should avoid the need to use Invoke or BeginInvoke within the Task. However, I'm not sure whether that will cover the EnableButtons(true) statement in the 'catch' clause as that isn't really part of the Task.
when you start the Task, from a Button_Click handler or wherever, to start it on a new threadpool thread, you'll now need to do:
Task.Factory.StartNew(() => InserClient(ClientId));
VulpesRank #55 · 29.2K reputationPosted May 8, 2011, 4:40 AM EDT
Antonios Dev37 reputationPosted May 8, 2011, 10:25 AM EDT
private void InserClient(int ClientId)
{
Task tsk;
tsk = Task.Factory.StartNew(() =>
{
AddClient(ClientId)
});
}
private void AddClient(ClientId)
{
try
{
some database code .......
}
catch (Exception ex)
{
this.BeginInvoke((System.Action)delegate
{
EnableButtons(true);
throw ex;
});
}
}