每段时间检查一次状况
我有一个特定的状态(业务案例),我想每隔一段时间检查一次,以执行一个操作..他们告诉我写一个补丁来处理这种情况..
我工作的应用程序是一个Web应用程序(asp .net)..
我不知道如何编写补丁,并且我不知道补丁是否是这种状态下的理想解决方案..
请提出任何建议,,对此问题的任何详细解释..
提前致谢。
i have a specific state(business case),i want to check it every period of time,to execute an action..they tell me to write a patch to handle this situation ..
the application i works in is a web application (asp.net)..
i don't know how to write the patch ,, and i don't know if the patch is the ideal solution in this state or not..
please any suggestions ,, any details explanation for this issue..
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,设置一个计时器来执行此检查非常简单。初始化一个计时器,
然后只需在
stateCheckTimer_Elapsed
函数中检查您的状态,最困难的事情是访问 _State,因此您可能必须将计时器放在与其相同的位置(或者将它通过了,无论有效)。不过,我建议采用事件驱动的解决方案。在处理状态的类上创建一个公共事件处理程序,
然后封装对状态变量(在本例中为
_State
)的访问,以便您控制它的设置。设置后,触发此事件。我通过一个属性来做到这一点,然后,您只需要连接一个事件句柄来执行您想要的操作,
并在该函数中执行您想要的操作,
(您必须通过 EventArgs args 传递状态,但这很漂亮简单易做)。这样,每当状态发生变化时,您将能够立即对其采取行动(发送电子邮件,执行任何操作),而不必每 x 秒或每分钟轮询一次状态。它将使用更少的资源,更具反应性(更快),并最终成为更简洁的代码!如果您能够这样做,我会强烈推荐它。
Firstly, it is quite simple to setup a timer to do this check. Initialise a timer,
Then just check your state in the
stateCheckTimer_Elapsed
function,The most difficult thing will be accessing the _State, so you'll probably have to put the timer in the same location as it (or have it passed, whatever works). I would suggest an event driven solution though. Make a public event handler on your class that handles the state,
Then, encapsulate the access to your state variable (in this example,
_State
) so that you control the setting of it. When it is set, fire off this event. I do this through a property,Then, you just need to wire up an event handle to execute your desired action,
And in that function execute your desired action,
(you will have to pass the state in through the EventArgs args but that is pretty simple to do). This way, whenever the state changes you will immediately be able to act upon it (send an email, do whatever it is) rather than having to poll the state every x seconds or minutes. It will use less resources, be more reactive (quicker), and ultimately be neater code! If you are able to do it this way, I would highly recommend it.
此检查是从浏览器还是服务器端应用程序完成的?
如果是从客户端(浏览器)完成的,那么您可以使用 JavaScript 查看这篇文章
如果您想从服务器端代码执行此操作,则可以使用线程执行此操作,让线程休眠一段时间,然后在唤醒时检查对象的状态
this checking is done from the browser or on the server side application ?
if it is done from the client( Browser ) than you can do it with JavaScript See this Post
If you want to do it from the Server Side code than , you can do it with a thread, make thread sleep for some time and when it's wake up than check the state of your object
要执行的操作是数据库操作吗,例如更新数据库中的某些行?如果是,您可以创建一个数据库作业来处理这种情况。
Is the action to be executed a database one, e.g. update some row in the database? If yes you can create a database job that handles this situation.
编写一个 24/7 在后台运行的小型简单服务怎么样?我认为这是最简单的解决方案。
What about writing a small simple service that works in the background 24/7. I think its the simplest solution.