简体   繁体   English

我应该在代码中使用thread.sleep或timer吗?

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

I have the following code which send out SMS to the subscribers. 我有以下代码将SMS发送给订户。 However, some SMS were rejected from the SMSGateway because I'm sending too many SMS at one time. 但是,有些短信被SMSGateway拒绝了,因为我一次发送了太多短信。 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. 假设这是一个服务风格的应用程序,没有用户界面,仅从数据库中获取数据并将其发送到SMS,那么Thread.Sleep(...)就可以了。

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. 如果此应用具有用户界面,并且您正在UI线程上运行此SMS发送代码,则Thread.Sleep(...)将阻止您的UI。 Actually, smsSender.sendSMS is probably already blocking your UI in this case! 实际上,在这种情况下,smsSender.sendSMS可能已经阻止了您的UI!

Refactoring so that this code is off the UI thread is the answer. 重构以使该代码不在UI线程中就是答案。 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. 而且您可以简单地通过使用计时器来做到这一点,尽管您将必须重构代码,以便将结果集缓存在本地对象中,并且计时器会遍历该集合,一次发送一个SMS。

In either case, I hope you don't have a lock on the database while you're sending SMSes. 无论哪种情况,我都希望您在发送SMS时不会对数据库有任何锁定。

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). 您的问题被标记为[asp.net],因此我假设您有一个网页,当该网页被请求时将发送许多SMS消息(例如,当用户按下“提交”按钮或HTML格式的类似内容时)。

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. 如果这样做,则在发送SMS消息时,用户将等待网页响应,这将有一个延迟。

I would suggest something like this: 我建议这样的事情:

  • When you need to send SMS messages you store the messages in a table in your database. 当您需要发送SMS消息时,会将消息存储在数据库的表中。
  • After storing new messages in the database you start a task ( Task.Factory.StartNew ) to process the SMS messages in the database. 在将新消息存储在数据库中之后,您将启动任务( Task.Factory.StartNew )以处理数据库中的SMS消息。
  • You need to make sure that no more than one task is running in the ASP.NET application. 您需要确保ASP.NET应用程序中没有运行多个任务。 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 ). 该任务将处理数据库中所有剩余的消息,并使用适当的延迟(由Thread.Sleeep完成)发送它们。
  • When the task has sent an SMS message it is removed from the database. 任务发送了SMS消息后,该任务将从数据库中删除。

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. Thread.Sleep似乎设计不好。 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. 请参阅http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program/了解Thread.sleep为何不好。

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至少在您指定的时间量内等待(操作系统可能使它进入睡眠时间更长)。 .

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

Thread.Sleep() should be a good choice to delay calling to SMS gateway to prevent server reject your request. Thread.Sleep()应该是延迟调用SMS网关以防止服务器拒绝您的请求的好选择。

I don't think it's Thread.Sleep() that's tying up the CPU. 我认为不是Thread.Sleep()占用了CPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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