Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:29:01+00:00 2026-06-01T22:29:01+00:00

im using following code to send sms to all the students in the database

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:29:03+00:00Added an answer on June 1, 2026 at 10:29 pm

    try this

    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++)
            {
                if ((bgw.CancellationPending == true))
                {
    
                    e.Cancel = true;
                    break;
                }
                else
                {
                    Invoke((MethodInvoker)delegate()
                    {
                        using (var sp = new SerialPort(cbcomport.Text))
                        {
                            try
                            {
                                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);
                            }
                            catch (Exception ex)
                            {
                                if (MessageBox.Show(ex.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry)
                                {
                                    try
                                    {
                                        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);
                                    }
                                    catch (Exception ex2)
                                    {
                                        MessageBox.Show(ex2.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        bgw.CancelAsync();
    
                                    }
                                }
                                else
                                {
                                    bgw.CancelAsync();
                                }
                            }
                        }
                    });
                }
            }
        }
        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (Control c in Controls)
            {
                c.Enabled = true;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sending sms to recipients using following code private void button1_Click(object sender, EventArgs
I'm using following code to send sms throuh gsm modem for (int i =
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I'm using the following code to send an sms. But this seems to not
i am using the following code to send request to the server var url='getDNormalForm.action';
I generate a vcard that I send to the client using the following code
I send the text file to multiple devices using the following code, ContentValues values
I am trying to send email using c# following is my code. try {
I am using the following code to send a user id when the button
I am using the following code to send an email with the contents of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.