我通过 gps 调制解调器通过 comport 使用以下代码发送 SMS
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 向其他收件人发送短信