none
Timer based Execution RRS feed

  • Question

  • Hi Experts, 

    I am working on some ETL task using C# and C# is fairly new to  me. I need to perform execution of certain step of querying SQL Server with different sets of query after every  3- 10 seconds. Could you please advise me some solution. 

    Let say 

    1. Query 1 --> Execute every 5 second

    2. Query 2 --> Execute every 3 second

    3. Query 3 --> Execute every 10 second

    4. Query 4 --> Every 2 second

    I am working on a windows service currently but how can I have timer based execution in the service for executing above Query 1-4 based on timer. Some example will surely support.

    Thanks in advace

    Priya


    • Edited by Priya Bange Wednesday, October 23, 2019 3:33 AM .
    Wednesday, October 23, 2019 3:33 AM

All replies

  • Try one of possibilities:

    var t1 = new System.Timers.Timer( TimeSpan.FromSeconds( 5 ).TotalMilliseconds );

    t1.Elapsed += MyFunction1;

    t1.Start( );

    . . .

    void MyFunction1( object sender, ElapsedEventArgs e )

    {

       . . .

    }

     

     

    The function MyFunction1 will be called periodically.

    Use a similar code for other timers.


    • Edited by Viorel_MVP Wednesday, October 23, 2019 4:57 AM
    Wednesday, October 23, 2019 4:57 AM
  • Try one of possibilities:

    var t1 = new System.Timers.Timer( TimeSpan.FromSeconds( 5 ).TotalMilliseconds );

    t1.Elapsed += MyFunction1;

    t1.Start( );

    . . .

    void MyFunction1( object sender, ElapsedEventArgs e )

    {

       . . .

    }

     

     

    The function MyFunction1 will be called periodically.

    Use a similar code for other timers.


    Hi , 

    Thank you so much for the quick support. Is there a way to do dynamically, like create memory table 

    load my queries into the memory table & every second a worker thread checks for the next query to executed using the For Each. Reason because I will be pulling the queries to be executed from another SQL Server table & the queries to be executed may increase /decrease.The SQL Server table along with query will also provide frequency. 

    So read from SQL Server --> Load into some memory table --> Execute the queries in the memory table --> Update the next time for execution in the memory table itself. 

    I am very new to C# but thinking of possible design patterns to support dynamic list of queries. 

    Thanks again

    Priya 

    Wednesday, October 23, 2019 5:18 AM
  • Hi Priya,

    Thank you for posting here.

    I don't quite understand the meaning of “create memory table” and “The SQL Server table along with query will also provide frequency.”

    Could you explain more in detail, or provide some code so that we can understand what you mean?

    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.

    Thursday, October 24, 2019 9:51 AM
  • Hi Priya,

    Thank you for posting here.

    I don't quite understand the meaning of “create memory table” and “The SQL Server table along with query will also provide frequency.”

    Could you explain more in detail, or provide some code so that we can understand what you mean?

    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.

    Hi Sir, 

    Let me explain, 

    I need to query data from SQL Server 1

    SQL Server 1


      SQL_Server_Instance Query Frequency (Sec)
     1 Local_Host_1 SELECT 1 FROM Table_1 3
     2 Local_Host_1 SELECT Columns FROM Table_2 4
     3 Local_Host_2 SELECT Columns FROM Table_2 2
     4 Local_Host_3 SELECT Columns FROM Table_2 5

    Load the data into some memory object in C# with a column for Next execution time. The queries need to be executed based on the Frequency. Let say, if we execute query 1 its next execution time will get updated in the C# data table & on next run referring to the table all the queries execution time matching the current time will again be fired. 

    The queries in the SQL Server 1 can increase or reduce also. 

    Thanks

    Priya

    Friday, October 25, 2019 2:56 AM
  • Could anyone please support based on my last shared details. 

    Thanks 

    Priya

    Saturday, October 26, 2019 6:46 PM