timer question

M

Mike P

I have a loop which goes thorough a recordset until a particular value
in the recordset changes. In situations where the value doesn't change,
I would like to use a timer to check whether 60 seconds have elapsed, in
which case I exit the loop.

I'm not really sure how to do this as I'm new to C#. I've tried to use
the Timer.Elapsed, but I'm not sure how I can use this condition to exit
my loop.


Any help would be much appreciated.


Mike
 
J

Jon Skeet

Mike P said:
I have a loop which goes thorough a recordset until a particular value
in the recordset changes. In situations where the value doesn't change,
I would like to use a timer to check whether 60 seconds have elapsed, in
which case I exit the loop.

I'm not really sure how to do this as I'm new to C#. I've tried to use
the Timer.Elapsed, but I'm not sure how I can use this condition to exit
my loop.
Any help would be much appreciated.

The easiest way would be to use DateTime.Now:

DateTime start = DateTime.Now;
TimeSpan timeout = new TimeSpan (0, 1, 0); // One minute

while (...)
{
if (DateTime.Now-start > timeout)
break;

// do whatever

Thread.Sleep (1000); // Don't just tight-loop!
}
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

I am curious, why are you wasting resources when you can just hook into
the event structure of the DataSet or DataTable.

You said "recordset", so the possibility exists that you are using an
ADO Recordset. If so, there are events that the ADO Recordset throws to
indicate that a change is taking place.

Either of these solutions would be better than wasting a thread polling
for changes and then sleeping in between polls.

Hope this helps.
 
M

Mike P

Thanks for your replies. Basically I am converting an ASP website
written in VB Script to ASP.NET with C#. Being my first project in
either ASP.NET or C#, I'm not very good with the terminology yet...when
I said recordset I meant the .NET equivalent!

The code that I was looking at that I thought might be a problem
converting was this :

Do While strNewStatus = "0" Or _
strNewStatus = "1"

rsTransaction.Close

Set rsTransaction = CreateObject("ADODB.Recordset")

rsTransaction.Open strTransaction & Session("SessionID") & "AND ID =
" & Session("ID"), connSession, adOpenStatic, _
adLockReadOnly, adCmdText
If Not rsTransaction.EOF Then strNewStatus =
rsTransaction.Fields("Status").Value
If timer() - StartTime >= 60 Then Exit Do
Loop

A VB6 application is running in the background which may or may not
change the strNewStatus value from it's default of 0 in the database on
the same machine. Therefore, I need some way of exiting the loop in
case this value does not change.

I have so far done a bit of work using data readers, and I don't forsee
having any problems with the data access side of things, although I
thought that I would have a problem with the timer side. Jon's reply
definitely worth looking into, though if you know of a better way to go
about this, then let me know.

Thanks for your time,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top