简体   繁体   中英

Should I use thread.sleep or timer in my code?

I have the following code which send out SMS to the subscribers. However, some SMS were rejected from the SMSGateway because I'm sending too many SMS at one time. So I'm thinking to make a delay in between.

Sending out the SMS like this -

            foreach (DataRow row in dt.Rows)
            {
                //Gets Subscriber number
                smsSender.destinationNum = Convert.ToInt64(row["callerID"]);
                foreach (DataRow articleRow in dtArticle.Rows)
                {
                    //Gets SMS content
                    smsSender.smsMessage = articleRow["news"].ToString();
                    //Then send out the SMS
                    smsSendder.sendSMS();
                }
            }

Please advice because I have no experience with the threads and timers

It would depend on the architecture of the application.

Assuming this is a service-style app, with no user interface, that simply gets data out of the database and sends it to SMS, then Thread.Sleep(...) is fine.

If this app has a user interface, and you're running this SMS sending code on the UI thread, then Thread.Sleep(...) will block your UI. Actually, smsSender.sendSMS is probably already blocking your UI in this case!

Refactoring so that this code is off the UI thread is the answer. And you can do that simply by using a timer, although you will have to refactor the code so that the result set is cached in a local object and the timer iterates through the set sending one SMS out at a time.

In either case, I hope you don't have a lock on the database while you're sending SMSes.

Your question is tagged [asp.net] so I assume that you have a webpage that when requested will send a number of SMS messages (eg when a user presses a "submit" button or something similar in a HTML form).

In that case you can have multiple users requesting the webpage simultaneously. Also, you don't want to sleep in the thread serving the web page to the user. If you do that then there will be a delay where the user waits for the web page to respond while the SMS messages are sent.

I would suggest something like this:

  • When you need to send SMS messages you store the messages in a table in your database.
  • After storing new messages in the database you start a task ( Task.Factory.StartNew ) to process the SMS messages in the database.
  • You need to make sure that no more than one task is running in the ASP.NET application. Storing new messages in the database involves checking if the task is running and if not starting it.
  • The task will process all remaining messages in the database and send them using the appropriate delay (done by Thread.Sleeep ).
  • When the task has sent an SMS message it is removed from the database.

This solution offloads the sending of messages to a background task that can be as slow as required and introduces persistence using the database to avoid loosing messages even if say the application pool is recycled.

Thread.Sleep seems bad design. Please refer http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program/ about why Thread.sleep is a bad.

Timer are more accurate, Thread.Sleep is only guaranteed to wait at LEAST as long as the amount of time you specify (the OS may put it to sleep for much longer). .

Thread.Sleep更合适,因为它可以更好地建模waiting方面。

Thread.Sleep() should be a good choice to delay calling to SMS gateway to prevent server reject your request.

I don't think it's Thread.Sleep() that's tying up the CPU.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM