Hi,
Can someone please clarify to me how background worker actually works. I would like to do a simple login form that will show login progress to the user after they click loggin since normally it will take long to read login details therefore I would like to update the user instead of letting the ui hang.
I have tried the code below but I am not sure how or where do I save the result so that I can display to the user if login is successfull or not.
Any assistance will be much appreciated.
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace LoginDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btncancel_Click(object sender, EventArgs e)
{
if(backgroundWorker1.WorkerSupportsCancellation == true){
backgroundWorker1.CancelAsync();
btnlogin.Enabled = true;
btncancel.Enabled = false;
}
}
private void btnlogin_Click(object sender, EventArgs e)
{
testobj.username = txtid.Text.Trim().ToString();
if (txtid.Text == string.Empty)
{
MessageBox.Show("Please enter username", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtid.Focus();
}
else
{
lblMessage.Visible = true;
progressBar1.Visible = true;
testobj.ReadData();
backgroundWorker1.RunWorkerAsync();
btnlogin.Enabled = false;
btncancel.Enabled = true;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int dataload = 0;
for (dataload = 0; dataload <= 100; dataload++)
{
if (backgroundWorker1.CancellationPending == true)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
break;
}
else {
backgroundWorker1.ReportProgress(dataload);
}
}
e.Result = dataload;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblMessage.Text = "Loading..." + e.ProgressPercentage.ToString() + " %";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Operation Cancelled ", "Cancelled ", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0;
lblMessage.Text = "";
progressBar1.Visible = false;
}
else if (e.Error != null)
{
MessageBox.Show(e.Error + "Error found " + e.Error.Message , "Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
progressBar1.Value = 0;
lblMessage.Text = "";
}
else {
MessageBox.Show("Progress done " + e.Result.ToString() + " ","Done ",MessageBoxButtons.OK,MessageBoxIcon.Information);
progressBar1.Value = 0;
lblMessage.Text = "";
}
}
private void ReadDatas() {
try {
Thread.Sleep(100);
}catch(Exception e){
MessageBox.Show("Login Result ", e.Message , MessageBoxButtons.OK,MessageBoxIcon.Information);
progressBar1.Value = 0;
lblMessage.Text = "";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
using System.Data;
namespace LoginDemo
{
class testobj
{
public static String username, lastname, firstname, status, lblMessage;
public static int progressBar1Value = 0;
public static string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\IMAC\APP\IMACDB\FormDB2.xlsx;Extended Properties='Excel 12.0;HDR=Yes;'";
public static OleDbConnection cnn;
public static OleDbCommand cmd;
public static OleDbDataAdapter da;
public static OleDbDataReader dr;
public DataSet dstResults = new DataSet();
public DataView myView;
public DataTable dtable;
public DataColumn c;
public static void ReadData()
{
try
{
string queryString = "SELECT Employee_ID,LastName,FirstName FROM [Entries$] WHERE Employee_ID ='" + username.Trim().ToString() + "'";
dr = null;
cnn = new OleDbConnection(connectionString);
cmd = new OleDbCommand(queryString, cnn);
cnn.Open();
dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
Thread.Sleep(100);
firstname = dr[1].ToString();
lastname = dr[2].ToString();
MessageBox.Show("Login successfull");
}
else
{
MessageBox.Show("Username Invalid", "Error Message");
}
dr.Close();
}
catch (Exception e)
{
MessageBox.Show("Error " + e.Message, "Error");
}
}
}
}