You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using ibk.IPD.Controller.IPD_PS;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Windows.Forms;
namespace ibk.IPD.Common
{
[RoutePrefix("ipd/ipdPs")]
[Synchronization()]
public class TimerController : ApiController
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static SurplusMaterialSubstitutionController surplusMaterialSubstitution = new SurplusMaterialSubstitutionController();
/// <summary>
/// 启动所有余材订单查询匹配的定时器
/// </summary>
/// <returns></returns>
[HttpPost, Route("startTimer")]
public void Timers()
{
//创建一个事件来指示超时计数阈值
//定时器回调。
AutoResetEvent autoEvent = new AutoResetEvent(false);
//创建需要定时器的类(展示用demo)
// StatusChecker statusChecker = new StatusChecker(10);
// 创建一个推断的委托,调用计时器的方法。(展示用demo)
//TimerCallback tcb = statusChecker.CheckStatus;
//创建余材替代推断的委托, 调用需要计时器的方法
TimerCallback tcb = SurplusMaterialSubstitutionController.AddSupersede;
// 创建一个指示委托调用的计时器。
logger.Info(string.Format("{0} Creating timer.\n", DateTime.Now.ToString("h:mm:ss.fff")));
System.Threading.Timer stateTimer = new System.Threading.Timer(tcb, autoEvent, 1000, 250);
//设置定时器多长时间执行一次(毫秒值)
stateTimer.Change(0, 60000);
//设置该线程的等待时间 -1 为无限 , 如果为true则在等待之前退出上下文的同步域如果处于一个同步的上下文,然后重新获取它 , 相反就是false
autoEvent.WaitOne(-1, false);
//摧毁定时器(停止)
stateTimer.Dispose();
logger.Info("\nDestroying timer : 定时器已销毁, 并停止运行.");
}
}
//展示用demo类
class StatusChecker
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private int invokeCount;
private int maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// 此方法由定时器委托调用。
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
logger.Info(string.Format("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString()));
}
}
}