im using following code to send sms to all the students in the database
private void btnsend_Click(object sender, EventArgs e)
{
foreach (Control c in Controls)
{
c.Enabled = false;
}
if (!bgw.IsBusy)
{
bgw.RunWorkerAsync();
}
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Invoke((MethodInvoker)delegate()
{
using (var sp = new SerialPort(cbcomport.Text))
{
sp.Open();
sp.WriteLine("AT" + Environment.NewLine);
sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
sp.WriteLine("AT+CMGS=\"" + dt.Rows[i]["PhoneNo"] + "\"" + Environment.NewLine);
sp.WriteLine(tbsms.Text + (char)26);
if (sp.BytesToRead > 0)
{
tbsentto.Text = i + 1 + " of " + dt.Rows.Count;
}
}
});
Thread.Sleep(5000);
}
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
foreach (Control c in Controls)
{
c.Enabled = true;
}
}
my question is: during sms is being sent to the students if an exception occurs e.g The port ‘COM5’ does not exist. i want to display the same system message to the user with buttons retry and cancel. if having resolved the problem. i.e having plugged in the device user presses retry button i want to resume the thread from the same point and if user presses cancel button i want to stop the paused thread.
try this