Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 553
paxcow Avatar asked Jul 24 '12 13:07

paxcow


2 Answers

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.

like image 91
mellamokb Avatar answered Oct 23 '22 17:10

mellamokb


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

like image 27
Massimiliano Peluso Avatar answered Oct 23 '22 16:10

Massimiliano Peluso