Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计时器至少终止一次,如何避免?_C#_Asp.net_Timer - Fatal编程技术网

C# 计时器至少终止一次,如何避免?

C# 计时器至少终止一次,如何避免?,c#,asp.net,timer,C#,Asp.net,Timer,我有一个定时器类,它以特定的间隔运行另一个控制器,并将数据发布到网站。但每天至少有一包不定期丢失。如何避免这种数据丢失 最初,我是在控制器执行时存储的,而不是在控制器未执行时存储的。它发出了一个错误,“远程主机关闭了连接”。我开始通过记录时间来验证计时器是否执行,如果有任何错误,它会记录异常和时间 简而言之, 我尝试记录错误,如果它转到catch块,但它甚至没有转到on timed event或timer类。onTimedEvent中的try块存储计时器执行时,catch块将错误存储在txt文件

我有一个定时器类,它以特定的间隔运行另一个控制器,并将数据发布到网站。但每天至少有一包不定期丢失。如何避免这种数据丢失

最初,我是在控制器执行时存储的,而不是在控制器未执行时存储的。它发出了一个错误,“远程主机关闭了连接”。我开始通过记录时间来验证计时器是否执行,如果有任何错误,它会记录异常和时间

简而言之, 我尝试记录错误,如果它转到catch块,但它甚至没有转到on timed event或timer类。onTimedEvent中的try块存储计时器执行时,catch块将错误存储在txt文件中

public class BaseTimer : Timer
{
    public BaseTimer(double Interval) : base(Interval) { }
    public Timer_class masterDataObj { get; set; }
}

public class Timer_class
{
    private static BaseTimer aTimer;

    public Timer_class()
    {
            double refreshTimeInterval = 5 * 60 * 1000;
            // Create a timer with a 5 minute interval.
            aTimer = new BaseTimer(refreshTimeInterval);
            // Hook up the Elapsed event for the timer. 
            aTimer.masterDataObj = this;
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {           
        try
        {
            //write time when timer is triggered in timelog folder
            string dbPath = "D:\\IIS Applications\\EAS_Wattics\\logs\\";//System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
            Directory.CreateDirectory(dbPath + "Energy_meter_Timelog");
            string FilePath = dbPath + "Energy_meter_Timelog//P" + DateTime.Now.ToString("yy") + DateTime.Now.ToString("MM") + DateTime.Now.ToString("dd")  + ".txt";
            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(fs);
                file.WriteLine(DateTime.Now.ToString());
                file.Close();
            }
            string result = EAS_Wattics_PostingController.post_to_wattics();
            string pbi = EAS_Wattics_PostingController.pushToPBI();
            //string missed_data = EAS_Wattics_PostingController.missed_data_post_to_wattics();
            string res_test = testController.post_to_wattics();
        }
        catch (Exception tmrEventExp)
        {
            string remark = "error :" + tmrEventExp;
            //write error and time of the error in errorlog folder
            string dbPath = "D:\\IIS Applications\\EAS_Wattics\\logs\\";//System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
            Directory.CreateDirectory(dbPath + "Energy_meter_Errorlog");
            string FilePath = dbPath + "Energy_meter_Errorlog//P" + DateTime.Now.ToString("yy") + DateTime.Now.ToString("MM") + DateTime.Now.ToString("dd") +  ".txt";
            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(fs);
                file.WriteLine(DateTime.Now.ToString() + remark);
                file.Close();
            }
            Debug.WriteLine("Exception occurred @Timer Event - Master Data Updation due to " + tmrEventExp.ToString());
        }
    }
}

我想每5分钟发送一次数据(代码在控制器中),但此计时器类至少会被终止一次。

不清楚,如果问题是“远程主机已关闭…”,则与计时器无关,它已执行,web服务器出错(或网络工作)。可以重试这类错误,取决于五分钟是多么重要。无关:考虑使用一个体面的日志框架。如果你的网站没有被经常使用,IIS将循环使用线程池。那个么你们的计时器就根本不会运行了(请参阅)。网站经常被使用。这没有问题@GSerg除了@GSerg所说的之外:默认情况下,de应用程序池也会在1740分钟后进行回收,无论使用情况如何。当这种情况发生时,会话状态被传输到新池,但运行的线程被终止。不清楚,如果问题是“远程主机已关闭…”,则与计时器无关,它已执行,web服务器出错(或网络工作)。可以重试这类错误,取决于五分钟是多么重要。无关:考虑使用一个体面的日志框架。如果你的网站没有被经常使用,IIS将循环使用线程池。那个么你们的计时器就根本不会运行了(请参阅)。网站经常被使用。这没有问题@GSerg除了@GSerg所说的之外:默认情况下,de应用程序池也会在1740分钟后进行回收,无论使用情况如何。当这种情况发生时,会话状态被传输到新池,但正在运行的线程被终止。