我应该在代码中使用 thread.sleep 还是计时器?

发布于 2025-01-06 03:12:38 字数 636 浏览 1 评论 0原文

我有以下代码,可以向订阅者发送短信。但是,一些短信被 SMSGateway 拒绝,因为我一次发送了太多短信。所以我想在这之间延迟一下。

像这样发送短信 -

            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();
                }
            }

请提供建议,因为我没有线程和计时器的经验

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

归途 2025-01-13 03:12:39

这取决于应用程序的架构。

假设这是一个服务式应用程序,没有用户界面,只是从数据库中获取数据并将其发送到 SMS,那么 Thread.Sleep(...) 就可以了。

如果此应用程序有用户界面,并且您在 UI 线程上运行此 SMS 发送代码,则 Thread.Sleep(...) 将阻止您的 UI。实际上,在这种情况下,smsSender.sendSMS 可能已经阻止了您的 UI!

答案是重构,使该代码脱离 UI 线程。您只需使用计时器即可做到这一点,尽管您必须重构代码,以便将结果集缓存在本地对象中,并且计时器会迭代该集合一次发送一条短信。

无论哪种情况,我希望您在发送短信时不要锁定数据库。

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.

月寒剑心 2025-01-13 03:12:39

您的问题被标记为 [asp.net],因此我假设您有一个网页,当请求时,该网页将发送大量 SMS 消息(例如,当用户按下“提交”按钮或 HTML 表单中的类似内容时)。

在这种情况下,您可以让多个用户同时请求该网页。另外,您不想在为用户提供网页的线程中休眠。如果您这样做,那么在发送 SMS 消息时,用户等待网页响应将会出现延迟。

我建议这样:

  • 当您需要发送短信时,您可以将消息存储在数据库的表中。
  • 在数据库中存储新消息后,您启动一​​个任务 (Task.Factory。 StartNew)来处理数据库中的短信。
  • 您需要确保 ASP.NET 应用程序中运行的任务不超过一项。在数据库中存储新消息涉及检查任务是否正在运行以及是否未启动。
  • 该任务将处理数据库中的所有剩余消息,并使用适当的延迟(由 Thread.Sleeep 完成)发送它们。
  • 当任务发送 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 (e.g. 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.

世态炎凉 2025-01-13 03:12:39

Thread.Sleep 更合适,因为它更好地模拟了等待方面。

Thread.Sleep is more appropriate, because it models better the waiting aspect.

昵称有卵用 2025-01-13 03:12:39

Thread.Sleep() 应该是延迟调用 SMS 网关以防止服务器拒绝您的请求的不错选择。

我不认为 Thread.Sleep() 占用了 CPU。

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.

树深时见影 2025-01-13 03:12:39

Thread.Sleep 似乎设计不好。请参考http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-Designed-program/ 了解原因Thread.sleep 很糟糕。

计时器更准确,Thread.Sleep 只能保证至少等待您指定的时间(操作系统可能会使其休眠更长时间)。 。

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). .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文