paxcow
paxcow

Reputation: 1941

how can i make an infinite loop with 5 second pauses

    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.

Upvotes: 7

Views: 27335

Answers (4)

Nathaniel
Nathaniel

Reputation: 457

Try:

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

Its the most simple.

Upvotes: 6

Martin James
Martin James

Reputation: 24857

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.

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

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

Upvotes: 6

mellamokb
mellamokb

Reputation: 56769

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.

Upvotes: 18

Related Questions