1

I want use one Timer in my controller to do a specific job.

Below is the code

    public class ProductsController : ApiController
    {

        private readonly System.Timers.Timer _checkTimer = new System.Timers.Timer();
        public readonly int CheckTimerInterval = 10 * 30 * 1000;

        public ProductsController()
        {
            _checkTimer.Elapsed += CheckTimerElapsed;
            _checkTimer.Interval = this.CheckTimerInterval;
            _checkTimer.Enabled = true;
        }

        private void CheckTimerElapsed(object source, ElapsedEventArgs e)
        { 
          //Do the processing
        }
     }

But the problem is when ever I call controller a new instance of Timer is created.

I want only one timer instance.Can you please help me to achieve this?

I know using Timer in controller is not a good idea but I do not have other option.I use this controller to assign Requests to an temporary User.In Timer I need to get all jobs and assign it to actual User.

5
  • 8
    "I know using Timer in controller is not a good idea" - Correct, it's not. What exactly are you trying to accomplish? Commented Dec 5, 2018 at 14:33
  • This is not a good idea. The only way I can see this working is if you add the timer to a dependency injection container as a singleton or making it static somewhere else - perhaps wrapped in a service Commented Dec 5, 2018 at 14:36
  • 7
    This is an XY problem. Rather than asking "I know this is bad but how can I make it work", explain why you need it in the first place and we can help you come up with an alternative that isn't bad. Commented Dec 5, 2018 at 14:36
  • I use this controller to assign Requests to an temporary User.In Timer I need to get all jobs and assign it to actual User. Commented Dec 5, 2018 at 14:43
  • 1
    I use this controller to assign Requests to an temporary User.In Timer I need to get all jobs and assign it to actual User. <= that still provides no context. What is the business use case. What is it your are trying to achieve. What is a temporary user, what is a job, why assign jobs to users, what does a timer have to do with jobs or users, etc. Commented Dec 5, 2018 at 14:46

2 Answers 2

3

You can use your same code in a static class under one method. Then call that method under Global.asax.cs file under your Application_Start() method

public static class GlobalValues
{
    private static System.Timers.Timer bomreporttimer;
    public static void StartScrapBom()
    {
        bomreporttimer = new System.Timers.Timer();
        bomreporttimer.Elapsed += Bomreporttimer_Elapsed;
        bomreporttimer.Interval = 1000 * 60 * 15;
        bomreporttimer.Enabled = true;
    }
}

Global.asax.cs file

 public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalValues.GlobalValues.StartScrapBom();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should not do this in the first place, try to find the problem you are trying to solve and try other options of solving it.

But in any case, you need to have a static instance of the timer, so you can simply make it static, you can use IOC as well to do that. It might be better to place it in a place like startup or global_asax kind of code, to indicate it's global and static. Btw, you need to think about threads as well, so may be make it a singeton.

Again, don't do this at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.