Dear All, Please need your support i dovleped one application and there while loop which keep runining and condition there is spcific time like if time reach 13:00 then go to next code its working fine no issue found but only one issue while loop keep runinig and chekcing time for this system process using like 25% and more how can i reduce this and or is there another way to run application with spcific time without consuming system high processing. please advice below code for your refrence.
C#
while (true)
{
if (System.DateTime.Now.ToString("HH:mm") == "13:15")
{
int i;
for (i = 0; i < 35; i++)
{
}
}
else
{ }
}
Amit MohantyRank #112 · 20K reputationPosted Jul 20, 2023, 10:30 AM EDT
You can use System.Timers.Timer or System.Threading.Timer to run a specific code block at a specific time without busy-waiting. Here's an example using System.Timers.Timer:
using System; using System.Timers; class Program { private static Timer timer; static void Main(string[] args) { // Set the desired time to trigger the event (13:15) DateTime targetTime = DateTime.Today.AddHours(13).AddMinutes(15); // Calculate the time difference between now and the target time double interval = (targetTime - DateTime.Now).TotalMilliseconds; // If the target time has already passed today, schedule it for the next day if (interval < 0) { targetTime = targetTime.AddDays(1); interval = (targetTime - DateTime.Now).TotalMilliseconds; } // Create a timer that will trigger the event at the target time timer = new Timer(interval); timer.Elapsed += Timer_Elapsed; timer.Start(); // Keep the application running Console.WriteLine("Application is running. Press any key to exit."); Console.ReadKey(); // Clean up resources when the application exits timer.Stop(); timer.Dispose(); } private static void Timer_Elapsed(object sender, ElapsedEventArgs e) { // Perform the task you want to execute at 13:15 here Console.WriteLine("Task executed at 13:15."); // You can add your code here instead of the dummy for loop } }Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jul 20, 2023, 3:57 PM EDT
You can achive this through timer class. Here is the code example:
static void Main() { // Define the time you want the code to run every day int targetHour = 15; // 3:00 PM int targetMinute = 30; // 3:30 PM // Calculate the time difference from now to the target time TimeSpan timeUntilTarget = CalculateTimeUntilTarget(targetHour, targetMinute); // Schedule the code to run at the target time every day Timer timer = new Timer(ExecuteCode, null, timeUntilTarget, TimeSpan.FromDays(1)); // Keep the program running until manually terminated Console.WriteLine("Code scheduled to run every day at {0}:{1}", targetHour, targetMinute); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } static TimeSpan CalculateTimeUntilTarget(int targetHour, int targetMinute) { DateTime now = DateTime.Now; DateTime targetTime = new DateTime(now.Year, now.Month, now.Day, targetHour, targetMinute, 0); if (targetTime < now) { targetTime = targetTime.AddDays(1); } return targetTime - now; } static void ExecuteCode(object state) { // Place your code here that you want to execute at the specified time every day Console.WriteLine("Code executed at: " + DateTime.Now); }Deepak RawatRank #646 · 3.7K reputationPosted Jul 21, 2023, 1:51 AM EDT
Timerclass in C#. can perform this task:using System; using System.Threading; public class MyScheduler { private Timer timer; public MyScheduler() { // Set the target time for the action (e.g., 13:15) DateTime targetTime = DateTime.Today.AddHours(13).AddMinutes(15); // Calculate the time remaining until the target time int dueTime = (int)targetTime.Subtract(DateTime.Now).TotalMilliseconds; if (dueTime < 0) { // If the target time has already passed for today, schedule it for tomorrow dueTime += 24 * 60 * 60 * 1000; } // Set up the timer to execute the desired action when the target time is reached timer = new Timer(DoAction, null, dueTime, Timeout.Infinite); } private void DoAction(object state) { // Your code to execute when the target time is reached (e.g., 13:15) int i; for (i = 0; i < 35; i++) { // Your logic here } // Reschedule the timer for the next occurrence (e.g., 24 hours later) timer.Change(24 * 60 * 60 * 1000, Timeout.Infinite); } }