Click here to Skip to main content
11,706,979 members (48,005 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 VS2010 WinForm Task , +
Good day all!!!

I have been struggling with understanding tasks.
I have a login form that has an authenticating gif that is displayed when the user clicks the login button. I decided to use a task so that the form ui doesn't freeze while attempting to login. I will add the code below.
My first question is:

01. Is my coding right Smile | :)

02. If there is an error while attempting to login (server side); how do I pass a custom exception message from the server to the task exception, so that I can access it using
result.Exception.Message

03. After some browsing and asking, I'm wondering if it would be better to use a BackgroundWorker. And if so, how would I go about doing that.

Thanx in advance for you help...

    List<SqlParameter> _SqlParameter = new List<SqlParameter>(7);
    CancellationTokenSource tokenSource = new CancellationTokenSource();
    CancellationToken token = tokenSource.Token;
 
    token.ThrowIfCancellationRequested();
 
    Task.Factory.StartNew(() =>
    {
        //bsiLoginStatus.Caption = "Authenticating...";

        SetBarStaticItemValue(bsiLoginStatus, "Authenticating...", ClaimCatch_Lite.Properties.Resources.loading);
 
        try
        {
            _SqlParameter.Add(new SqlParameter("@User_UserName", SqlDbType.VarChar, 50));
            _SqlParameter.Add(new SqlParameter("@User_Password", SqlDbType.VarChar, 50));
            _SqlParameter.Add(new SqlParameter("@Station", SqlDbType.VarChar, 50));
            _SqlParameter.Add(new SqlParameter("@Users_RowID", SqlDbType.Int, 100, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, 0));
            _SqlParameter.Add(new SqlParameter("@Users_Name", SqlDbType.VarChar, 100, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, string.Empty));
            _SqlParameter.Add(new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, 0));
            _SqlParameter.Add(new SqlParameter("@Default_Message", SqlDbType.VarChar, 200, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, string.Empty));
 
            _SqlParameter[0].Value = tbUsername.Text.Trim();
            _SqlParameter[1].Value = Encryption.EncryptData(tbPassword.Text.Trim());
            _SqlParameter[2].Value = Environment.MachineName.Trim();
 
            SqlDatabase.ExecuteReader("user_login", CommandType.StoredProcedure, _SqlParameter);
 
            GlobalClass.User_UserName = _SqlParameter[0].Value.ToString();
            GlobalClass.User_RowID = Convert.ToInt32(_SqlParameter[3].Value);
            GlobalClass.Users_Name = _SqlParameter[4].Value.ToString();
        }
        catch (Exception ex)
        {
            tokenSource.Cancel(true);
            DialogResult show = XtraMessageBox.Show(ex.Message, "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }).ContinueWith(result =>
    {
        DialogResult show;
 
        switch (result.Status)
        {
            case TaskStatus.Canceled:
                SetBarStaticItemValue(bsiLoginStatus, result.Exception.Message, ClaimCatch_Lite.Properties.Resources.exclamation);
                show = XtraMessageBox.Show(result.Exception.Message, "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 
                break;
            case TaskStatus.Faulted:
                SetBarStaticItemValue(bsiLoginStatus, result.Exception.Message, ClaimCatch_Lite.Properties.Resources.exclamation);
                show = XtraMessageBox.Show(result.Exception.Message, "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 
                break;
            case TaskStatus.RanToCompletion:
                if (Convert.ToBoolean(_SqlParameter[5].Value))
                {
                    CloseThis();
 
                    SetBarStaticItemValue(bsiLoginStatus, "Login successful...", ClaimCatch_Lite.Properties.Resources.accept);
 
                    Thread _ApplicationThread = new Thread(ShowApplication) { IsBackground = true, Name = "ApplicationThread" };
                    _ApplicationThread.Start();
                }
                else
                {
                    SetBarStaticItemValue(bsiLoginStatus, _SqlParameter[6].Value.ToString(), ClaimCatch_Lite.Properties.Resources.exclamation);
                    show = XtraMessageBox.Show(Convert.ToString(_SqlParameter[6].Value), "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
 
                break;
            default:
                SqlDatabase.DestroyConnectionString();
                Application.Exit();
 
                break;
        }
    },
        token,
        TaskContinuationOptions.None,
        TaskScheduler.FromCurrentSynchronizationContext());
}
Posted 26-Mar-13 22:25pm
azinyama1.2K
Edited 26-Mar-13 22:27pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 165
1 OriginalGriff 125
2 chainerlt 110
3 ppolymorphe 89
4 Mika Wendelius 70
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100