Hello all,
I am trying to create a login form. The problem that I am having is that the login process is taking too long and is locking up my GUI. I have read up on background worker, but I am still uncertain on how to have my program wait for the login process but not freeze my GUI. Here is my code to help explain it more.
Login.cs
public partial class Login : Form
{
public delegate bool Authenicate(string account, string password,string type);
public Authenicate authenicate;
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtAccount.Text == string.Empty)
{
MessageBox.Show("Must include account number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (txtPassword.Text == string.Empty)
{
MessageBox.Show("Must include password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!authenicate(txtAccount.Text, txtPassword.Text,cmbType.Items[cmbType.SelectedIndex].ToString()))
{
return;
}
this.DialogResult = DialogResult.OK;
}
private void Login_Load(object sender, EventArgs e)
{
cmbType.SelectedIndex = 0;
}
MainForm.cs
public partial class MainForm: Form
{
Ex.Service myService=new Ex.Service();
public MainForm()
{
InitializeComponent();
}
public bool Authenicate(string account, string password,string type)
{
try
{
//Login takes too long and locks up GUI
//Maybe try background worker, but how to wait until
//login is complete?
myService.Login(account,password,type);
return myService.IsLogin();
}
catch(Exception exception)
{
MessageBox.Show(exception.message);
}
return false;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
myService.Logout(); //Logout from service
myService = null;
}
}
Thank you for your time.