简体   繁体   English

如何每天重置应用程序变量

[英]How to reset an application variable daily

I am writing a program recording service calls and treatment done. 我正在写一个程序记录服务电话和治疗完成。 We have a number of users who open and close calls and I want to show at all times the total number of calls opened today and the total number closed today and the difference between them. 我们有许多用户打开和关闭来电,我想随时显示今天开通的电话总数和今天关闭的总数以及它们之间的差异。 I thought of doing it with an application variable. 我想过用一个应用程序变量来做。 I have to reset these variables to 0 every day. 我必须每天将这些变量重置为0。 Where would I do that? 我会在哪里这样做? I thought in the Global.asax but in which event could that be done? 我在Global.asax中想过,但在哪个事件可以做到? The application is running all the time so I suppose Application_Start wouldn't be appropriate. 应用程序一直在运行,所以我认为Application_Start不合适。 So where? 那么在哪里? Thank you. 谢谢。

I'd have a date variable with the last time the counter was reset, and check the date is "today" on every access to the counter. 我有一个日期变量,上次重置计数器,并在每次访问计数器时检查日期是“今天”。

Unless you have critical performance problems, I guess that'd be the way to go. 除非你有严重的性能问题,否则我想这就是要走的路。

Sample easy-lazy code to call whenever you are updating the counter: 在更新计数器时调用简单易用的代码示例:

lock(myCounter)
{ 
  if(DateTime.Now.Date != lastDateCounterWasReset)
  {
     lastDateCounterWasReset = DateTime.Now.Date;
     myCounter = 0;
  }
  myCounter++;
}

Now we'd need to know more about how you'd like to be storing those variables ( myCounter and lastDateCounterWasReset ), but could be basically anywhere (database, filesystem, etc.) 现在我们需要了解更多关于如何存储这些变量的信息( myCounterlastDateCounterWasReset ),但基本上可以在任何地方(数据库,文件系统等)

You could configure the Periodic Restart Settings for Application Pool Recycling in IIS: 您可以在IIS中为应用程序池回收配置定期重新启动设置

The element contains configuration settings that allow you to control when an application pool is recycled. 该元素包含允许您控制应用程序池何时被回收的配置设置。 You can specify that Internet Information Services (IIS) 7 recycle the application pool after a time interval (in minutes) or at a specific time each day. 您可以指定Internet Information Services(IIS)7在一段时间间隔(以分钟为单位)或每天的特定时间后回收应用程序池。 You can also configure IIS to base the recycle on the amount of virtual memory or physical memory that the worker process in the application pool is using or configure IIS to recycle the application pool after the worker process has processed a specific number of requests. 您还可以配置IIS以使回收基于应用程序池中的工作进程正在使用的虚拟内存或物理内存量,或者在工作进程处理了特定数量的请求后将IIS配置为回收应用程序池。

But this has a side-effect of putting the application offline during the time the pool is restarting, so if you have any user connected at that time it will lose its session. 但是这会产生副作用,即在池重新启动时将应用程序置于脱机状态,因此如果您在此时连接了任何用户,则会丢失其会话。 This can be minimized by restarting the application at a time you have no users connected, like at dawn. 这可以通过在没有用户连接的情况下重新启动应用程序来最小化,例如在黎明时。

The following config snippet set the application pool to daily recycle at 3:00 AM: 以下配置代码段将应用程序池设置为每天凌晨3:00回收:

<add name="Example">
   <recycling logEventOnRecycle="Schedule">
     <periodicRestart>
       <schedule>
          <clear />
          <add value="03:00:00" />
        </schedule>
     </periodicRestart>
   </recycling>
 <processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>

I would store the calls to a database and do a select which groups by the current day to get the total calls, etc. for display. 我会将呼叫存储到数据库并按当前日期选择哪些组来获取总呼叫等以供显示。

That way it will automatically reset for you when a new day starts, and you don't need to worry about IIS Resets destroying your in memory data. 这样,它会在新的一天开始时自动重置,您无需担心IIS Resets会破坏您的内存数据。

If you don't want the performance hit of querying too often, there are a number of caching options available. 如果您不希望过于频繁地查询性能,则可以使用许多缓存选项。

I suppose you could use the Application_BeginRequest method. 我想你可以使用Application_BeginRequest方法。 Use a boolean to see if it's already run that day. 使用布尔值来查看它是否已在当天运行。

Another option is a scheduler with a hidden URL to reset. 另一个选项是具有要重置的隐藏URL的调度程序。

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

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