Problem with IIS releasing timer object when using System.Threading.Timer object in Project

Source: Internet
Author: User
Tags iis log log log

There was a problem with IIS releasing the timer object when using the System.Threading.Timer object in one of the previous projects. Honestly, I've never had this problem before. That is, I previously defined the timer object is not released, running normally, I came back after the Baidu search for this information, the original IIS in the run WebApp for non-static resources are automatically released, and I looked back at the previous written Web program, Luckily it was written in this way:

Global.asax file

private static Timer time; System.Threading;
private static log log;
protected void Application_Start (object sender, EventArgs e)
{
    log = new log ();
    Log. Write (ref time, 5000);
}

Log.cs Code:

Class log{public  
        void Write (ref Timer Time,int flashtime)
        {
            if (time = = null) {Time
                = new Timer (New Ti Mercallback (doexecution), this, 0, 

flashtime);
            }
        void Doexecution (Object obj)
        {
            //Timed execution Code
        }
}

That is to say, the timer object is defined as a global static object will not be released by IIS, if not so write, will certainly be depressed for a while when the error. But now a wide range of knowledge, scheduled to perform tasks can use quartz.net open source components, he encapsulated the time object, you can make the task more stable execution, the following show sample code:

public class TimeJob:Quartz.IJob {public
    void execute (quartz.jobexecutioncontext context)
    {
        //code to be executed regularly ...
    }
}
public class Global:System.Web.HttpApplication
{
    private static Timer time;
    protected void Application_Start (object sender, EventArgs e)
    {
        //define task
        quartz.jobdetail job = new Quartz.jobdetail ("Job1", "group1", typeof

(Timejob));
        Define trigger
        Quartz.trigger Trigger = Quartz.TriggerUtils.MakeSecondlyTrigger (5);//5 seconds to

execute
        Trigger. Name = "Trigger1";
        Trigger. Jobgroup = "Group1";
        Trigger. JobName = "Job1";
        Trigger. Group = "Group1";
        The definition plans
        quartz.ischedulerfactory SF = new Quartz.Impl.StdSchedulerFactory ();
        Quartz.ischeduler sch = sf. Getscheduler ();
        Sch. AddJob (Job, true);//Add Task
        Sch. Schedulejob (trigger);//Add Planner
        Sch. Start ();//Begin Execution
    }
}

The above code is also defined in the Global.asax file.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.