I'm curious about the logic that checks only once at a certain time

I would like help.

There are some problems with the code. I want to do only one inspection at a certain time every day.

In particular, the problem is the most serious in DateTime.Now.Hour == 11 part. I am having difficulty checking certain times. I need to write code that can be checked only once at 11:00 in the whlie statement.

I created a license file and checked the date of the file.

public static CResult Dailytime(string date)
        {
            CResult result = new CResult();

            if(result.nRet == 0)
            {
                while (true)
                {
                    if (result.nRet == 1 || result.nRet == 2)
                    {
                        return result;
                    }

                    if (DateTime.Now.Hour == 11)
                    {
                        result = DailyCheckData(date);

                        if(result.nRet == 1 || result.nRet == 2)
                        {
                            return result;
                        }
                    }
                    System.Threading.Thread.Sleep(60 * 30 * 1000);
                }
            }
            return result;
        }

        public static CResult DailyCheckData(string data)
        {
            CResult result = new CResult();

            DateTime licenseDate = Convert.ToDateTime(data); 
            string dateNow = DateTime.Now.ToString("yyyy-MM-dd"); 

            int compareDate = DateTime.Compare(Convert.ToDateTime(data), DateTime.Now);

            if (licenseDate.ToString("yyyy-MM-dd") == dateNow)
            {
                result = ExpirationCertificate();
                Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);

                return result; 
            }
            else
            {
                if (compareDate > 0)
                {
                    result = TrueCertificate();
                    Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);

                }
                else if (compareDate <= 0)
                {

                    result = ExpirationCertificate();
                    Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);
                }
                return result;
            }
        }

CResult class
nRet= 0 or 1 or 2

0 = fasle date
1 = false file
2 = true

Suggest or suggest ways to improve.

Can you try create a variable for DateTime.Now, after a line of code that value change.

DateTime licenseDate = Convert.ToDateTime(data); 
string dateNow = DateTime.Now.ToString("yyyy-MM-dd"); 
int compareDate = DateTime.Compare(Convert.ToDateTime(data), DateTime.Now);

To

DateTime licenseDate = Convert.ToDateTime(data);
var now = DateTime.Now;
string dateNow = now.ToString("yyyy-MM-dd");
int compareDate = DateTime.Compare(licenseDate, now);

I (Kendrick Lamar song), I definition, the ninth letter of the English alphabet, a vowel. See more. Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics.

you shouldn't use Thread.Sleep() method for such a long duration. It is poor programming logic to make the thread sleep for such a long period.

What you can do to solve this is, is to make a Timer. There's an example attached in the link. A simple snippet to match your logic would be:

licenseStart = //setYours
lastCheck = DateTime.Now;
nextCheck = now.AddDays(1); // now.AddHours(1)
var timer = new Timer(o=>{/* Do work*/}, null, TimeSpan.Zero, nextCheck);

Hope it helps!

I, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Bir hesap oluştur veya Instagram'a giriş yap - Fotoğraf ve videolar çekip düzenlemenin, bu fotoğrafları, videoları ve mesajları arkadaşlarınla ve akrabalarınla paylaşmanın basit, eğlenceli ve yaratıcı yolu.

I asked about logic that can only loop once at 11 o'clock. But I could not find the right answer and I found the answer.

I do not speak English well. That's why others do not understand the intent of the question.

bool bOnce = true;
        //bool s0nce = true;

while (true)
{
    if (DateTime.Now.Hour == 11  && bOnce)
    {
          //run code
    }
    if (bOnce == true)
        bOnce = false;
    else
        bOnce = true;
    Thread.Sleep(10 * 60 * 1000);
}

I, Windows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark of Apple Inc. This will clear your Bing search history on this device. You can't undo it.

[PDF] USCIS Form I-9, Use Form I-9 to verify the identity and employment authorization of individuals hired for employment in the United States. Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.

I-9, Employment Eligibility Verification, Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family. iStock. Explore the official iStock website for millions of exclusive, royalty-free, stock files. Find the perfect stock for your project, fast. Search now.

Instagram, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? IMDb is the world's most popular and authoritative source for movie, TV and celebrity content. Find ratings and reviews for the newest movie and TV shows. Get personalized recommendations, and learn where to watch across hundreds of streaming providers.

Comments
  • As an aside, look into Windows Scheduler
  • Can you clarify exactly what the problem is? What is happening? What is the desired behavior?
  • I would like to do DailyCheckData CLASS work once at Dailytime CLASS at 18:00.
  • You need to explain exactly what you mean by "I am having difficulty checking certain times."
  • "DateTime.Now.Hour == 11" portion of WHILE LOOP can be received one time in case of SLEEP for 30 minutes, and it may come in 2 times. It can be passed in 40 minutes setting.
  • there is not problem. DateTime.Now.Hour == 11 is problem. System.Threading.Thread.Sleep(60 * 30 * 1000); I want to create a role like a scheduler that works at 11 o'clock.
  • May be this link help you about simple scheduler stackoverflow.com/questions/3243348/…
  • I don't understand.
  • okay, now I understand your problem, its because you're returning the result once from an infinite while loop, to avoid this, make the result object static(global), or pass the result object to DailyTime function, where you can keep updating it periodically with the help of a timer method