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 :)
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(() =>
{
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());
}