C#: How to start a thread at a specific time

Bob

How can I start a background thread at a specific time of day, say 16:00?

So when the apps starts up the thread will wait until that time. But if the app starts up after that time then the thread will run straight away

ThreadPool.QueueUserWorkItem(MethodtoRunAt1600);

Sriram Sakthivel

You can set up a timer at 16:00. I've answered a similar question here. That should help you for sure.

private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
     DateTime current = DateTime.Now;
     TimeSpan timeToGo = alertTime - current.TimeOfDay;
     if (timeToGo < TimeSpan.Zero)
     {
        return;//time already passed
     }
     this.timer = new System.Threading.Timer(x =>
     {
         this.SomeMethodRunsAt1600();
     }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

private void SomeMethodRunsAt1600()
{
    //this runs at 16:00:00
}

Then set it up using

SetUpTimer(new TimeSpan(16, 00, 00));

Edit: Keep the reference of the Timer as it's subject to garbage collection irrespective of the Timer is active or not.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to compare start and end time with current time in php?

分類Dev

bootstrap-datepicker: How to use specific time zone?

分類Dev

How to select rows which values start and end with a specific value in pandas?

分類Dev

Thread how to change sleep time

分類Dev

How to calculate time difference between start time and end time by group in pandas?

分類Dev

How do I grab a process's start time in minutes?

分類Dev

How to set a flag in docker at the time of start

分類Dev

How can I invert the count and set the countdown start time?

分類Dev

How can I track the time pods are waiting to start?

分類Dev

How is the best way to play an MP3 file with a start time and end time in Python 3

分類Dev

How to design dag for different scheduler start time

分類Dev

How to check a date time column in a specific range in python panda?

分類Dev

How to extract a double quote field in regex that start with a specific string

分類Dev

Executing a function on a specific thread in C++

分類Dev

threading.Thread.start() method execution time depends on the Thread target method

分類Dev

How to measure the time it took for a thread to get past a condition variable?

分類Dev

How spark calculates the window start time with given window interval?

分類Dev

WPF: How to call a method at a specific point in time while an animation is running

分類Dev

How to start applications automatically when Windows starts with a time window

分類Dev

How to start apps always on specific display?

分類Dev

Qt, start and stop Thread with signals

分類Dev

C#: How to start a thread at a specific time

分類Dev

How to fix a specific time for a method in java

分類Dev

How to Start Service and Stop Service Based on Time Interval in android

分類Dev

How to limit a thread's execution time and terminate it if it runs too long?

分類Dev

How to find out when a specific thread exits (C#)?

分類Dev

POSIX Thread Running Time And Attribute Display In C

分類Dev

How to create a survival time variable based on start and stop time

分類Dev

How do I get my Activity Indicator to start at the right time?

Related 関連記事

  1. 1

    How to compare start and end time with current time in php?

  2. 2

    bootstrap-datepicker: How to use specific time zone?

  3. 3

    How to select rows which values start and end with a specific value in pandas?

  4. 4

    Thread how to change sleep time

  5. 5

    How to calculate time difference between start time and end time by group in pandas?

  6. 6

    How do I grab a process's start time in minutes?

  7. 7

    How to set a flag in docker at the time of start

  8. 8

    How can I invert the count and set the countdown start time?

  9. 9

    How can I track the time pods are waiting to start?

  10. 10

    How is the best way to play an MP3 file with a start time and end time in Python 3

  11. 11

    How to design dag for different scheduler start time

  12. 12

    How to check a date time column in a specific range in python panda?

  13. 13

    How to extract a double quote field in regex that start with a specific string

  14. 14

    Executing a function on a specific thread in C++

  15. 15

    threading.Thread.start() method execution time depends on the Thread target method

  16. 16

    How to measure the time it took for a thread to get past a condition variable?

  17. 17

    How spark calculates the window start time with given window interval?

  18. 18

    WPF: How to call a method at a specific point in time while an animation is running

  19. 19

    How to start applications automatically when Windows starts with a time window

  20. 20

    How to start apps always on specific display?

  21. 21

    Qt, start and stop Thread with signals

  22. 22

    C#: How to start a thread at a specific time

  23. 23

    How to fix a specific time for a method in java

  24. 24

    How to Start Service and Stop Service Based on Time Interval in android

  25. 25

    How to limit a thread's execution time and terminate it if it runs too long?

  26. 26

    How to find out when a specific thread exits (C#)?

  27. 27

    POSIX Thread Running Time And Attribute Display In C

  28. 28

    How to create a survival time variable based on start and stop time

  29. 29

    How do I get my Activity Indicator to start at the right time?

ホットタグ

アーカイブ