By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,671 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,671 IT Pros & Developers. It's quick & easy.

timer question

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Mike P <mr*@telcoelectronics.co.uk> wrote:
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!
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #3

P: n/a
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.