定时器无法正常工作

时间:2015-10-12 05:56:53

标签: c# windows service timer

定时器只能工作一次。这项服务必须每2分钟工作一次...... public partial class Service1:ServiceBase     {         RuleContext entity = new RuleContext();         private int id;         私人定时器_timer;         private DateTime _lastRun = DateTime.Now.AddDays(-1);

    public Service1()
    {
        InitializeComponent();
    }



    protected override void OnStart(string[] args)
    {
        OnTimer();
    }

    public void OnTimer()
    {
        Timeout.Infinite);
        _timer = new Timer();
        _timer.Interval = 2 * 60 * 1000;
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        _timer.Enabled = true;
        _timer.AutoReset = true;
        _timer.Start();

    }



    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {

        // ignore the time, just compare the date
        if (_lastRun.Date <= DateTime.Now.Date)
        {


            GetRule();




        }
    }



    protected override void OnStop()
    {
    }

    public void GetRule()
    {



        var query = from ruleset in entity.RuleSets
                    join rule in entity.Rules on ruleset.Id equals rule.RuleSetId
                    join schedulerule in entity.Schedules on rule.ScheduleId equals schedulerule.Id

                    select new
                    {
                        Id = ruleset.Id,
                        daily = schedulerule.Daily,
                        mountly = schedulerule.Monthly,
                        dayofMounth = schedulerule.DayOfMonth,
                    };


        foreach (var q in query.ToList())
        {

            if (q.mountly && q.daily)
            {
                if (q.dayofMounth == (int)DateTime.Now.Day)
                {
                    UpdateValue(q.Id);
                }
            }

            else if (q.daily)
            {
                UpdateValue(q.Id);
            }

            else if (q.mountly)
            {
                if (q.dayofMounth == (int)DateTime.Now.Day)
                {
                    UpdateValue(q.Id);
                }
            }
        }
    }


    public void UpdateValue(int id)
    {
        var ruleSet = entity.RuleSets.First(k => k.Id == id);
        ruleSet.RcvByte = 0;
        ruleSet.SentByte = 0;
        entity.SaveChanges();
    }
}

2 个答案:

答案 0 :(得分:0)

private void timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)     {

//mainform.js

var app=angular.module("myapp",['ngRoute','ngDialog','ngSanitize','addfrmapp','homeapp']);

app.controller("controller", ["$scope","ngDialog",function($scope,ngDialog){

    $scope.getname=function(){

        $scope.value=$("#fmname").val();

    };

    $scope.setname=function(){
        $("#fmname").val("");
    };

    $scope.inputhtml=function(){
    console.log("hi");
   };
}]);
app.config(function($routeProvider){
    $routeProvider.when("/Home",
            {
                templateUrl:"Htmlfiles/Home.html"

            })
        .when("/Add",
                {
                    templateUrl:"Htmlfiles/AddForm.html",
                    controller:"addform"
                })
        .when("/View",
                {
                    templateUrl:"Htmlfiles/ViewForm.html"
                });
        $routeProvider.otherwise({ 
            templateUrl:"Htmlfiles/Home.html"
        });
});

//addform.js

var app=angular.module("addfrmapp",['ngDialog']);
app.controller("addform",['$scope','ngDialog',function($scope,ngDialog){

    $scope.$parent.value="";
    ngDialog.open({

        template: 'Htmlfiles/formname.html',
        closeByEscape:false,
        closeByDocument:false,
        scope: $scope,
       showClose:false


    });
    $scope.savebtn=function(){

        if($("#fmname").val()=="")
            {
                alert("Form name must be given in the input box");
            }
        else{
            $scope.$parent.getname();
            ngDialog.closeAll();
        }
    };
    $scope.cancelbtn=function(){

                $scope.$parent.setname();

    };

    $scope.addtext=function(){


                ngDialog.open({
                    template:"popupfiles/textpopup.html",
                    closeByDocument:false,
                    closeByEscape:false,

                    controller:['$scope',function($scope){
                        $scope.expndtxt=function(){

                            $scope.$parent.inputhtml();
                        };
                    }]

                });
            };  
            $scope.addradio=function(){

                ngDialog.open({
                    template:"popupfiles/radiopopup.html",
                    closeByDocument:false,
                    closeByEscape:false,
                });

            };
            $scope.addcheckbox=function(){

                ngDialog.open({
                    template:"popupfiles/checkboxes.html",
                    closeByDocument:false,
                    closeByEscape:false,

                });

            };
            $scope.addsubmitbutton=function(){

                ngDialog.open({
                    template:"popupfiles/submitbuttons.html",
                    closeByDocument:false,
                    closeByEscape:false,

                });

            };


}]);

由于AutoReset属性设置为true,因此无需停止和启动计时器。

答案 1 :(得分:0)

holder[]

此行导致此问题。删除它