7
    string connectionstring = "server =SQLVS2\\SQLVS2;database=DDM;Persist Security Info=True;uid=ddmuser;password=User02+Ddm;";
    SqlConnection myConnection = new SqlConnection(connectionstring);
    SqlCommand myCommand = new SqlCommand("GetNullHash", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myConnection.Open();
    SqlDataReader rdr = myCommand.ExecuteReader();

   while (rdr.Read())
    {
        string filename = @"\\" + rdr.GetString(3);
        filename = System.IO.Path.Combine(filename, rdr.GetString(2));
        filename = System.IO.Path.Combine(filename, rdr.GetString(1));
        computeHashh1 abc = new computeHashh1();
        Console.WriteLine(abc.computeHash(filename));          
        inserthash insert = new inserthash();
       insert.InsertHash(rdr.GetString(1), abc.computeHash(filename), rdr.GetStr(2),rdr.GetString(3));

    }

how can i make this loop run once in 5 seconds for ever or untill stopped.

paxcow
  • 1,670
  • 3
  • 17
  • 32

4 Answers4

18

The simplest method would be:

while (true) {
    // code here
    Thread.Sleep(5000);
}

However, for more robustness, I would recommend a windows service with a proper timer.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • how to write a proper timer in windows service? im actually using windows service in this but dont have a timer – paxcow Jul 24 '12 at 14:01
  • Take a look at the link I have on `proper timer`. – mellamokb Jul 24 '12 at 14:01
  • In both cases by stopping the service. – mellamokb Jul 24 '12 at 14:07
  • How can there be a lack of robustness to a sleep() loop? It doesn't need any extra timer thread or thread pool timer task to be raised and so eliminates two context switches. – Martin James Jul 24 '12 at 14:43
  • @MartinJames: I didn't know until the first comment above that the OP had a windows service. I was assuming a console app/windows forms app and comparing that to a windows service. A windows service is more robust in the sense that if you have it set to auto-start, it starts up again with every reboot. – mellamokb Jul 24 '12 at 14:45
6

Try:

while(true) 
{
    try 
    {
       thread.sleep(5000) 
    } 
    catch(Exception e) 
    {
      //handle the exception 
    }
}

Its the most simple.

Nathaniel
  • 457
  • 5
  • 14
  • Why is a timer a better approach? – Martin James Jul 24 '12 at 14:44
  • See this - has a nice explanation http://stackoverflow.com/questions/391621/compare-using-thread-sleep-and-timer-for-delayed-execution – Nathaniel Jul 24 '12 at 17:10
  • yr. ref. is not raelly relevant here. The OP is not continually creating threads, just looping on one thread started in a service. I challenge anyone to find a timer alternative to sleep() that is more efficient, CPU-wise, needs no other threads to run, does not require message-handling loops that are messy to implement if you do not need them for any other reason, will work anytime, in any thread, in any function, no matter how deeply nested in your thread code. – Martin James Jul 24 '12 at 19:04
  • Edited. I would agree that for what OP wants you are definitely right. – Nathaniel Jul 24 '12 at 20:22
6

I would wrap your code and use a timer object which will allow you to have control on the timer itself (stop,start,etc)

System.Timers.Timer aTimer;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    //your code
}

more info at

http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.100%29.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

Well, if I was going to do this, and the constraints on the interval are not precise, and I wanted to be absoultely sure that multiple timer events will never cause my code to be reentered if my code occasionally took longer than the interval, I would just use a sleep() loop and have the thing working without any extraneous timers that just add complexity to a what seems to be a simple requirement.

Outside of a GUI thread that must process messages, (or main service thread that must process start/stop etc), timers are often inappropriate, awkward or, if another thread/pool has to raise the timer event, wasteful of context-switches.

There is no problem, on Vista/W7 anyway, with stopping a service that has spawned threads that are sleeping. It takes about 20 seconds for the OS to realise that, even though the service main thread has handled the stop message, the service process still has blocked threads - it then terminates the service process anyway, killing any sleeping/running/waiting/whatever threads.

Martin James
  • 24,453
  • 3
  • 36
  • 60