-2

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.

dev_O
  • 153
  • 2
  • 10
  • As an aside, look into Windows Scheduler – CoolBots Feb 15 '19 at 05:00
  • 2
    Can you clarify exactly what the problem is? What is happening? What is the desired behavior? – CoolBots Feb 15 '19 at 05:03
  • I would like to do DailyCheckData CLASS work once at Dailytime CLASS at 18:00. – dev_O Feb 15 '19 at 05:12
  • You need to explain exactly what you mean by _"I am having difficulty checking certain times."_ –  Feb 15 '19 at 05:21
  • "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. – dev_O Feb 15 '19 at 05:32

3 Answers3

0

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);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • 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. – dev_O Feb 15 '19 at 04:41
  • May be this link help you about simple scheduler https://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c – Hien Nguyen Feb 15 '19 at 05:04
0

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!

kowsikbabu
  • 499
  • 1
  • 6
  • 23
  • I don't understand. – dev_O Feb 15 '19 at 05:35
  • 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 – kowsikbabu Feb 15 '19 at 05:49
0

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);
}
dev_O
  • 153
  • 2
  • 10