none
Timer Execution from App.Config RRS feed

  • Question

  • Hi Experts, 

    I wish to execute the below method say every 15- 30 seconds.

    Please support

    1. How can I set a timer object for this method.

    2. How can it be parameterize the timer frequency from from the App.config.

    3. If a session is already active shouldn't create a new session for the same. 

         public bool Start()
            {
    
                ConnectDB();
    
                return true;
            }

    Thanks 

    Priya

    Thursday, November 28, 2019 8:03 AM

Answers

  • Hi Priya,

    Thank you for posting here.

    For your question, you want to set a timer for your method and read time interval from app.config.

    For the timer, the format of its callback method is certain, so I made some changes to your method.

    Here is a code example.

            static void Main(string[] args)
            {
                int interval = int.Parse(ConfigurationManager.AppSettings["interval"]);
                System.Threading.Timer timer = new System.Threading.Timer(Start,null,1,interval);
    
                Console.ReadKey();
            }
    
            private static void Start(object state)
            {
                Console.WriteLine("Do something here.");
            }
    
    

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
        </startup>
      <appSettings>
        <add key="interval" value="1500"/>
      </appSettings>
    </configuration>
    

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 29, 2019 6:09 AM