安排在C#中使用Windows服务发送消息的最佳方法(Best way to schedule messages to be sent using a Windows service in C#)

编程入门 行业动态 更新时间:2024-10-23 15:22:30
排在C#中使用Windows服务发送消息的最佳方法(Best way to schedule messages to be sent using a Windows service in C#)

我会尽量简要地解释我期望达到的目标。 我这样做的初步想法在我看来并不奏效,所以我正在试图决定如何最好地规划这一点。

首先想到的是:

我有一个需要在预定时间发送的消息列表,每个消息都存储在一个中央SQL数据库中。

目的是使用Windows服务,该服务将有一个每30分钟计时一次的计时器。 所以..

30 Mins pass > Call ScheduleMessages()

ScheduleMessages将检查数据库以查找在接下来的30分钟内需要发送的任何未发送的消息,然后将它们标记在数据库中,如下所示:

ScheduleActivated = 1

对于标记为ScheduleActivated = 1的每一个,它将触发一个从正常计时器继承的客户时间对象,该对象还包括它需要发送的消息的属性。

它将被设置为在消息发出时刻打勾,将发送消息并在数据库中标记为成功。

这个问题的主要问题是我会在所有地方都有定时器,并且如果一次发送几百条消息,它可能会表现不佳,或者完全失败。

在重新评估之后,我想到了解决方案2

我的另一个想法是在服务中运行1个计时器,每10分钟打一次计时器。 每次它都会触发一个方法,收集每一条消息的方法,因为在任何时候都会将它发送到列表中,然后逐个处理它们。

这似乎要少得多的资源密集型,但我担心,如果计时器在10分钟后嘀嗒,任何尚未完成发送的消息都将在下一个嘀嗒中被捕获,并被再次发送。

一旦它已经持续了10分钟就停止定时器,然后重置为零,并且一旦消息已经发送就重新开始。

是否有第三种解决方案比上述更好?

I'll try and briefy explain what i'm looking to achieve. My intial idea of doing this, is not going to work well in my opinion, so i'm trying to decide how best to plan this.

The first thought was:

I have a list of messages that need to be sent out at scheduled times, each one is stored in a central SQL database.

The intention is to use a Windows service that will have a timer that ticks every 30 mins. So..

30 Mins pass > Call ScheduleMessages()

ScheduleMessages will check the database for any unsent messages that need to go out in the next 30 minutes, it will then mark them in the database as:

ScheduleActivated = 1

For each one it marks as ScheduleActivated = 1 it will fire off a customer time object which inherits from the normal timer, which also includes the properties for the message it needs to send.

It will be set to tick at the time the message is due to go out, will send the message and mark as successful in the database.

The main problem with this is I am going to have timers all over the place, and if there were a few hundred messages sheduled at once, it's probably going to either not perform well, or fall over completely.

After re-evalutating I thought of solution 2

My other idea was to have 1 timer running in the service, which ticks once every 10 minutes. Each time it ticks it would fire off a method that gathers every single message due to be sent at any point up until that time into a list, and then processes them one at a time.

This seems much less resource intensive, but i'm worried that if the timer ticks after 10 minutes, any messages that haven't finished sending, wil be caught in the next tick, and be sent again.

Would it be work to stop the timer once it has been going for 10 minutes, then reset to zero and start again once the messages have been sent.

Is there a 3rd solution to the problem which is better than the above?

最满意答案

我们在一个项目上实现了这一点,对我们来说有效的是:

所有消息都以发送时间写入表中 检查每个x分钟是否有要发送的服务 当服务发送消息时,它也会标记发送的消息(从null更新发送时间到实际发送时间)

标记消息可避免重新发送,如果您想重新发送,只需将日期设置为空。

我们遇到的唯一问题是服务作为单个线程运行,因此发送的消息数量受到限制。 但在这是一个问题之前,你会有非常多的消息和一个非常小的窗口。

We implemented this on one project, what worked for us was:

All messages written to a table with a send time Service that checks every x mins if there is something to send When the service sends a message it also marks the message a sent (update sent time from null to actual sent time)

Marking the message avoids resends, and if you want to resend you just set the date to null.

The only problem that we had is that the service ran as a single thread and the number of messages sent was therefore limited. But you would have very many messages and a very small window before this is a problem.

更多推荐

本文发布于:2023-07-17 07:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:排在   发送消息   方法   Windows   service

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!