Click here to Skip to main content
15,897,371 members

Loading Data Using Tasks

azinyama asked:

Open original thread
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:

C#
private DataTable LoadTable01() {//load data using stored procedure in to datatable and return datatable}
private DataTable LoadTable02() {//load data using stored procedure in to datatable and return datatable}
private DataTable LoadTable03() {//load data using stored procedure in to datatable and return datatable}
private DataTable LoadTable04() {//load data using stored procedure in to datatable and return datatable}
private DataTable LoadTable05() {//load data using stored procedure in to datatable and return datatable}
...


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.

C#
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);

//how do I catch the exception and send it to task result to exit the application???

                return _DataTable;
            }
            catch (Exception ex)
            {
//send exception to task
                //throw new OperationCanceledException(tokenSource.Token);
                //throw new OperationCanceledException("Error occurred while loading genders: " + ex.Message);
            }
        }


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???
Tags: C# (C# 4.0), TaskScheduler

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900