How do I activate Timespan with a set time

Asked

Viewed 59 times

0

I wanted to do a Timespan set, like this from 8:00 to 8:10 and after 16:00 to 16:10 he keeps sending a message that I created, outside of these times is not to appear.

var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(1);
{
    var timer = new System.Threading.Timer((e) =>
    {
        MessageBox.Show("Teste de Mensagem");
    },
    null, startTimeSpan, periodTimeSpan);
}

Ae I wanted to put this condition on some if.

1 answer

0


from what I understand you want this:

 var timer = new System.Threading.Timer((e) =>
                {
                    if ((DateTime.Now.TimeOfDay >= System.TimeSpan.Parse("08:00:00.000") && DateTime.Now.TimeOfDay <= System.TimeSpan.Parse("08:10:00.000")) 
|| (DateTime.Now.TimeOfDay >= System.TimeSpan.Parse("16:00:00.000") && DateTime.Now.TimeOfDay <= System.TimeSpan.Parse("16:10:00.000")))
                        {
                            MessageBox.Show("Teste de Mensagem");
                        }
                    },
                    null, startTimeSpan, periodTimeSpan);
  • I think the logic of his programming is right, but he keeps showing the message at any period, not at certain

  • @Wesleyhenrique I saw the problem now, it is about my copy and Paste

  • @Wesleyhenrique Try now, i just correct the >= and the time of 16

  • I fixed it and it worked, thanks man

  • @Wesleyhenrique marks the answer as accepted, left side checkbox :)

  • Will I be able to implement in an Asp.net system to send an email in that period?

  • @Wesleyhenrique yes.

  • And I would have to start it on App_start, with the email configuration?

  • in place of msgbox will call the function to do the email sending.

  • That’s what I did, I’ll test to see if it works only on the server and not on every machine the system opens

  • On Asp.net does not work.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.