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
VulpesPosted May 3, 2011, 2:28 PM
Antonios DevPosted May 8, 2011, 2:25 PM
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;
});
}
}
VulpesPosted May 8, 2011, 8:40 AM
VulpesPosted May 8, 2011, 8:06 AM
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));
Antonios DevPosted May 8, 2011, 7:30 AM
Thanks
Antonios DevPosted May 8, 2011, 7:13 AM
VulpesPosted May 8, 2011, 6:39 AM
Antonios DevPosted May 8, 2011, 4:31 AM
This thing started to confuse me. TPL was supposed to be much easier to use that traditional Threading yet is so unclear and confusing.
VulpesPosted May 7, 2011, 6:56 PM
Antonios DevPosted May 7, 2011, 6:15 PM
VulpesPosted May 7, 2011, 2:53 PM
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 DevPosted May 7, 2011, 10:10 AM
VulpesPosted May 7, 2011, 9:28 AM
Antonios DevPosted May 7, 2011, 4:38 AM
catch (Exception ex)
{
EnableButtons(true);
throw ex.Flatten();
}
Sam HobbsPosted May 6, 2011, 7:05 PM
Antonios DevPosted May 6, 2011, 6:48 PM
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);
}
}
Antonios DevPosted May 6, 2011, 12:53 PM
I enclosed the result of the Task into Try/Catch which catches AggregateException and Flattens it.