本帖最后由 huoshan12345 于 2014-1-22 15:57 编辑
我做了一个qq登录程序,点击登陆后对列表中的每一个qq执行登陆函数,如果对于其中的一个qq需要输入验证码,则暂停登陆,等待用户输入验证码后,然后继续登陆
我按照百度到的方法(下面的链接),套用了一下,结果出问题了,主窗体被锁住了,根本没办法输入
http://www.cnblogs.com/Fooo/archive/2008/12/27/1363619.html
[C#] 纯文本查看 复制代码 public partial class MainForm : Form
{
public WQQHelper wqq = null;
List<WQQHelper> WQQList = new List<WQQHelper>();
public MQQHelper mqq = null;
List<MQQHelper> MQQList = new List<MQQHelper>();
private delegate void DLogin();
AutoResetEvent VerifyCodeInputed = new AutoResetEvent(false);
private void button_Login_Click(object sender, EventArgs e)
{//点击登陆后
Thread th = new Thread(new ThreadStart(Login));
th.IsBackground = true;
th.Start();
}
private void Login()
{//登陆函数
if (this.InvokeRequired)
{
DLogin d = new DLogin(Login);
this.Invoke(d); return;
}
foreach (WQQHelper wqq in WQQList)
{
if (wqq.status.statusType != Status.StatusType.Offline)
{
continue;
}
textBox_VerifyCode.Text = "";
int index = listView_QQList.FindItemWithText(wqq.Number).Index;
listView_QQList.Items[index].Selected = true;
listView_QQList.Items[index].Focused = true;
Status status = wqq.Login(null);
if (status.statusType == Status.StatusType.LoginSuccessd)
{
new KeepAliveTrigger(wqq);
listView_QQList.Items[index].SubItems[4].Text = wqq.LoginMethod;
}
else if (status.statusType == Status.StatusType.NeedVerify)
{
listView_QQList.Items[index].SubItems[4].Text = "需要验证码";
this.pictureBox_VerifyPic.Image = wqq.GetVerifyImage();
VerifyCodeInputed.WaitOne(); // 等候输入验证码
this.wqq.Login(textBox_VerifyCode.Text);
listView_QQList.Items[index].SubItems[4].Text = status.StatusInfo;
}
else
{
listView_QQList.Items[index].SubItems[4].Text = status.StatusInfo;
}
}
}
private void textBox_VerifyCode_TextChanged(object sender, EventArgs e)
{
if (textBox_VerifyCode.Text.Length == 5)
{
VerifyCodeInputed.Set(); // 当输入的文本为5位,发一个通知
}
}
}
}
如果变成下面的这个样子,那么整个界面上的所有按钮都不能点了,文本框无法输入,表现的就像是锁住了一样
小程序测试:功能,先点击计算,然后输入,当输入的文本超过4位时,在输出中显示输入的内容
[C#] 纯文本查看 复制代码 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.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private delegate void DLogin();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(YourThread);
th.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length >= 4)
{
detailCollectedEvent.Set(); // 当textBox1的文本超过4位,发一个通知
}
}
AutoResetEvent detailCollectedEvent = new AutoResetEvent(false);
void YourThread()
{
detailCollectedEvent.WaitOne(); // 等候通知
textBox2.Text = textBox1.Text;
}
}
}
|