每段时间检查一次状况

发布于 2024-10-04 14:11:52 字数 193 浏览 4 评论 0原文

我有一个特定的状态(业务案例),我想每隔一段时间检查一次,以执行一个操作..他们告诉我写一个补丁来处理这种情况..

我工作的应用程序是一个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 技术交流群。

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

发布评论

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

评论(4

温柔嚣张 2024-10-11 14:11:52

首先,设置一个计时器来执行此检查非常简单。初始化一个计时器,

int _State;
System.Timers.Timer stateCheckTimer = new System.Timers.Timer();
stateCheckTimer.Interval = 1000; // TODO - set to desired interval (in ms)
stateCheckTimer.AutoReset = true;
stateCheckTimer.Elapsed += stateCheckTimer_Elapsed;

然后只需在 stateCheckTimer_Elapsed 函数中检查您的状态,

void stateCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
  // Check for the desired state
  if (_State == 1)
  {
    // Do something
  }
}

最困难的事情是访问 _State,因此您可能必须将计时器放在与其相同的位置(或者将它通过了,无论有效)。不过,我建议采用事件驱动的解决方案。在处理状态的类上创建一个公共事件处理程序,

public EventHandler OnStateChanged;

然后封装对状态变量(在本例中为_State)的访问,以便您控制它的设置。设置后,触发此事件。我通过一个属性来做到这一点,

public int State
{
  get { return _State; }
  set
  {
    _State = value;
    if (OnStateChanged != null)
    {
      OnStateChanged(this, null);
    }
  }
}

然后,您只需要连接一个事件句柄来执行您想要的操作,

OnStateChanged += StateChangeAction;

并在该函数中执行您想要的操作,

void StateChangeAction(object sender, EventArgs args)
{
  // Check for the desired state
  if (_State == 1)
  {
    // Do something 
  }
}

(您必须通过 EventArgs args 传递状态,但这很漂亮简单易做)。这样,每当状态发生变化时,您将能够立即对其采取行动(发送电子邮件,执行任何操作),而不必每 x 秒或每分钟轮询一次状态。它将使用更少的资源,更具反应性(更快),并最终成为更简洁的代码!如果您能够这样做,我会强烈推荐它。

Firstly, it is quite simple to setup a timer to do this check. Initialise a timer,

int _State;
System.Timers.Timer stateCheckTimer = new System.Timers.Timer();
stateCheckTimer.Interval = 1000; // TODO - set to desired interval (in ms)
stateCheckTimer.AutoReset = true;
stateCheckTimer.Elapsed += stateCheckTimer_Elapsed;

Then just check your state in the stateCheckTimer_Elapsed function,

void stateCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
  // Check for the desired state
  if (_State == 1)
  {
    // Do something
  }
}

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,

public EventHandler OnStateChanged;

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,

public int State
{
  get { return _State; }
  set
  {
    _State = value;
    if (OnStateChanged != null)
    {
      OnStateChanged(this, null);
    }
  }
}

Then, you just need to wire up an event handle to execute your desired action,

OnStateChanged += StateChangeAction;

And in that function execute your desired action,

void StateChangeAction(object sender, EventArgs args)
{
  // Check for the desired state
  if (_State == 1)
  {
    // Do something 
  }
}

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

逆夏时光 2024-10-11 14:11:52

此检查是从浏览器还是服务器端应用程序完成的?

如果是从客户端(浏览器)完成的,那么您可以使用 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

┈┾☆殇 2024-10-11 14:11:52

要执行的操作是数据库操作吗,例如更新数据库中的某些行?如果是,您可以创建一个数据库作业来处理这种情况。

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.

时光与爱终年不遇 2024-10-11 14:11:52

编写一个 24/7 在后台运行的小型简单服务怎么样?我认为这是最简单的解决方案。

What about writing a small simple service that works in the background 24/7. I think its the simplest solution.

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