我通过GPS调制解调器使用下面的代码通过COMPORT发送短信如何保持禁用,直到一个线程控制结束
Thread thread = null;
private void btnsend_Click(object sender, EventArgs e)
{
if (thread == null)
{
thread = new Thread(SendSms);
thread.IsBackground = true;
thread.Start();
}
}
private void Update(int i)
{
if(InvokeRequired)
{
this.BeginInvoke(new Action<int>(Update), new Object[] {i});
return;
}
using (var sp = new SerialPort("COM6"))
{
sp.Open();
sp.WriteLine("AT" + Environment.NewLine);
sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
sp.WriteLine("AT+CMGS=\"" + dt2.Rows[i]["PhoneNo"] + "\"" + Environment.NewLine);
sp.WriteLine(tbsms.Text + (char)26);
if (sp.BytesToRead > 0)
{
tbsentto.Text = i + 1 + " of " + dt2.Rows.Count;
}
}
}
private void SendSms()
{
for(int i = 0; i < dt2.Rows.Count; i++)
{
Update(i);
Thread.Sleep(5000);
}
thread = null;
}
我的问题是:我怎么能保持btnsend禁用,直到我的线程是进程,以便用户无法按btnsend向其他收件人发送短信
您可以检查[Thread.IsAlive(http://msdn.microsoft.com/ru-ru/library/system.threading.thread.isalive.aspx) – JleruOHeP 2012-04-05 07:06:14
你需要一个“线程完成“事件。 BackgroundWorker有一个。 – 2012-04-05 08:05:39