我想創建一個登錄表單。我遇到的問題是登錄過程耗時過長,並且鎖定了我的GUI。我已經閱讀了背景工作者,但我仍然不確定如何讓我的程序等待登錄過程,但不凍結我的GUI。這裏是我的代碼,以幫助更多地解釋它。C#關於如何防止GUI凍結的問題,但仍然處於阻塞狀態
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;
}
}
謝謝您的時間。
謝謝你的明確和有益的答案。 – Dylan 2010-01-28 05:33:19