none
Thread.Sleep from custom time RRS feed

  • Question

  • I have setup a Task Scheduler to run every hour, on the hour. Every so often as you can imagine there is some time lapse by a couple of seconds or more throughout the day.

    When the above task runs it stores in DateTime value in our database i.e. 01-10-2019 10:00:00, I'd imagine this is around the same time as a 3rd party service recording the time this program accessed the data. 

    There are some conditions i need to meet, the stored DateTime is the same time this 3rd party service stores. If i try to access the service again before the time expires then it would error out and i have to wait until the recorded time has expired.

    I've tried a couple of quick fixes which dont work. So i added Thread.Sleep which has its own limitations, but what i would like to do is get the stored Datetime from my server IF its equal to or greater than the CURRENT time then add 10 seconds (this is to cater for any lapse between the time stored on our server or theres.

    How could i do this?

    Tuesday, October 1, 2019 9:54 AM

Answers

  • Hi Pure Deal,

    Thank you for posting here.

    Another way for your reference. It is similar.

    Use DateTime.Subtract Method, it alseo could return TimeSpan. Here is the code.

      System.DateTime date1 = new System.DateTime(2019, 10, 2, 22, 15, 0);
                System.DateTime date2 = new System.DateTime(2019, 10, 2, 21, 15, 0);           
                System.TimeSpan diff1 = date2.Subtract(date1);

    Or you could make the TimeSpan to milliseconds.

      var diff2 = (int)date2.Subtract(date1).TotalMilliseconds;

    Best Regards,

    Wendy


    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.

    • Marked as answer by Pure Deal Wednesday, October 2, 2019 8:11 AM
    Wednesday, October 2, 2019 5:02 AM
    Moderator

All replies

  • Without code to get an initial idea what you have tried it's a guessing game to recommend something.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 1, 2019 10:10 AM
    Moderator
  • Actually i figured out to set the Timespan with the value from the server then add XX amount of time and pass that into Thread.Sleep.

    Let me know if you feel theres a better way

    Tuesday, October 1, 2019 12:27 PM
  • Hi Pure Deal,

    Thank you for posting here.

    Another way for your reference. It is similar.

    Use DateTime.Subtract Method, it alseo could return TimeSpan. Here is the code.

      System.DateTime date1 = new System.DateTime(2019, 10, 2, 22, 15, 0);
                System.DateTime date2 = new System.DateTime(2019, 10, 2, 21, 15, 0);           
                System.TimeSpan diff1 = date2.Subtract(date1);

    Or you could make the TimeSpan to milliseconds.

      var diff2 = (int)date2.Subtract(date1).TotalMilliseconds;

    Best Regards,

    Wendy


    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.

    • Marked as answer by Pure Deal Wednesday, October 2, 2019 8:11 AM
    Wednesday, October 2, 2019 5:02 AM
    Moderator
  • Thanks! thats similar to what i had. I'll mark yours as an answer.
    Wednesday, October 2, 2019 8:10 AM