views:

364

answers:

3

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.

A: 

I would say create an event in the login form and subscribe in the main form. Within the login form you can use the thread to perform login task if it is taking too long. Once login is succesful or unsuccessful you can notify using this event to main form and send the additional information in the event arguments to the main form.

Upon recieving this event, main form can then proceed based on the conditions set by you.

Kavitesh Singh
A: 

Disable the pertinent UI elements (buttons, textfields, etc.) and then spin up a background worker thread. When it completes, update the UI as appropriate.

Communicating with the UI could take the form of LoginSucceeded and LoginFailed events, or similiar.

Kevin Montrose
+1  A: 

The general steps are:

  1. Add the Background Worker to your Login dialog
  2. Create an event handler for the Background Worker's DoWork event that calls you authenticate delegate.
  3. In btnLogin_Click disable the Login dialog so the user cannot click login again while the back ground worker is running.
  4. In btlLogin_Click call the BackGround worker's RunWorkAsync method to start the worker running.
  5. Create an event handler for the Background Worker's RunWorkerCompleted event. In that event enable the LoginForm and either close the dialog if the login result was successful or display en error message.
shf301
Thank you for your clear and helpful answer.
Dylan