Good day all!!!
VS2010 C#4.0
I have 10 tables in an MSSQL database that I want to load into my application.
I have the following methods in my application:
private DataTable LoadTable01() {
private DataTable LoadTable02() {
private DataTable LoadTable03() {
private DataTable LoadTable04() {
private DataTable LoadTable05() {
...
Now I want to use Tasks to load the data tables. How would I go about doing this.
Reason for wanting to use tasks is that I want to be able to monitor each load method to make sure it loaded correctly, show the status of the load in a label field; i.e.:
"Loading table 01..."
"Loading table 01 complete..."
"Loading table 02..."
"Loading table 02 complete..."
...
And if any of the tables fails to load successfully Task stops loading any of the other remaining tables.
Thanx in advance...
Thanx for the response...
This is what I now have.
TaskScheduler context = TaskScheduler.FromCurrentSynchronizationContext();
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
token.ThrowIfCancellationRequested();
Task.Factory.StartNew(() =>
{
GlobalClass.Bank = LoadBank(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.BloodGroup = LoadBloodGroup(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.Gender = LoadGender(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.MaritalStatus = LoadMaritalStatus(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.MedicalCondition = LoadMedicalCondition(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.MemberSearch = LoadMemberSearch(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.Modifier = LoadModifier(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.PatientSearch = LoadPatientSearch(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.ProviderCategory = LoadProviderCategory(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.RejectionCode = LoadRejectionCode(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.Relationship = LoadRelationship(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.Title = LoadTitle(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.TaskCategory = LoadTaskCategory(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.TaskPriority = LoadTaskPriority(tokenSource);
}, token).ContinueWith(result =>
{
GlobalClass.UserStatus = LoadUserStatus(tokenSource);
}, token).ContinueWith(result =>
{
DataTable menu = LoadUserMenu(tokenSource);
LoadDynamicMenu(menu);
}, token).ContinueWith(result =>
{
switch (result.Status)
{
case TaskStatus.Canceled:
SetSplashScreenText("Error occurred while loading defaults: " + result.Exception.Message);
Thread.Sleep(4000);
Application.ExitThread();
Application.Exit();
break;
case TaskStatus.Faulted:
SetSplashScreenText("Error occurred while loading defaults: " + result.Exception.Message);
Thread.Sleep(4000);
Application.ExitThread();
Application.Exit();
break;
case TaskStatus.RanToCompletion:
break;
default:
break;
}
}).Wait();
private static DataTable LoadGender(CancellationTokenSource tokenSource)
{
SetSplashScreenText("Loading defaults: Genders...");
Thread.Sleep(100);
try
{
DataTable _DataTable = new dsDefault.GenderDataTable();
_SqlDataAdapter = SqlDatabase.FillSqlDataAdapter("load_gender", CommandType.StoredProcedure);
_SqlDataAdapter.Fill(_DataTable);
return _DataTable;
}
catch (Exception ex)
{
}
}
It works the way I want. But now I have another question.
How do I catch the exception and send it to task result to exit the application???