views:

49

answers:

4
+1  Q: 

Windows service

Hi all..

I have written onw windows service..in that i have logic that is some part of code need to execute on certain time.. my service is running in every one min..

e.g.

If (DateTime.Now.ToString("MM/dd/yyyy hh:mm") = "7/23/2010 1:10 ") Then

    'execute this logic

End If

But iam facing prob that it is considering seconds while running so can not compare above time...

Request you to suggest some other way..

+3  A: 

I am assuming you are running in a loop and comparing against current time - this is a busy wait and not the recommended way of running timed work.

Use a timer in your service and set the interval to 60000 milliseconds. Put the code that needs to run in the tick event.

See this article about the different timer classes in .NET.

Oded
same thing i did..but still facing above problem..
@amitpatil200 - you do have an extra space in the string you compare to. Is that intentional? You are also not using the comparison operator (`==`), but a single `=`.
Oded
@Oded: in VB you only use one =. :)
Leniel Macaferi
@Leniel Macaferi - got my C# hat on ;)
Oded
A: 

Would this solve your problem?

If (DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") = "7/23/2010 1:10:00") Then

    'execute this logic

End If
Leniel Macaferi
+3  A: 
DateTime checkTime = new DateTime(2010, 23, 7, 1, 10, 0);
DateTime now = DateTime.Now;
if(now >= checkTime && now < checkTime.AddSeconds(60)) 
{ ... }

Try to avoid using ToString as this type of comparission you can compare datetimes explicitly

aqwert
+1  A: 
DateTime target = DateTime.Parse("7/23/2010 1:10");
if (DateTime.Now >= target) { ... }

So, your code will execute next cycle after the target time (of course, you need to make sure it runs exactly once, if it is what you need).

VladV