R
R
rinnn2018-08-25 19:54:09
C++ / C#
rinnn, 2018-08-25 19:54:09

How to execute code at specific time?

var a = DateTime.Parse("17:45");
                if (DateTime.Now == a);
            {
                
                MessageBox.Show("1");
            }

For example, you need to show the message box at 17:45. If the current time on the computer matches var "a", then the code is executed. The problem is that the code is executed anyway, even if the time doesn't match. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Peter, 2018-08-25
@rinnn

You can try that.

if(DateTime.Now.Hour == 21)
            {
                MessageBox.Show("Test");
            }

M
MIsternik, 2018-08-25
@MIsternik

DateTime date = DateTime.Parse("17:45");
Task.Delay(date - DateTime.Now ).ContinueWith(MessageBox.Show("1"));

R
Roman Mirilaczvili, 2018-08-25
@2ord

https://www.codeproject.com/Tips/1214782/How-to-Sc...

L
leremin, 2018-08-25
@leremin

if (DateTime.Now == a);
Firstly, the semicolon must be removed, and secondly, it is better to do something like this: if ((DateTime.Now - a).TotalSeconds < 1), for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question