Something I really could do with finding out...

Posted by dragon on Saturday, 24 September 2011

... But I don't know how.

Here are a couple of things I know:
It's a shady proposition setting a timer running on a form, and doing a straight method call to update the form's display. It works sometimes, but because the timer runs (or at least can run ) on another thread to the rest of the form, it sometimes falls over.

There are a few nice elegant ways of sorting this all out and getting everything running properly in .NET 1.x, but .NET 2.0 brings with it the BackgroundWorker class.

Which is a nice thing, particularly if you want to do some long running processes and update your UI as they go.

This I know.

But what I don't know is: what does the timer do when it's not elapsing? Obviously it's ticking away, just... well... being a timer, but is that it?

What I'm getting at (albeit in a roundabout and thoroughly confused way) is this:

I have a countdown timer that updates the form every second. I can't just use a timer to do it and handle its Elapsed event, but what if I fired up a BackgroundWorker, and then just called System.Threading.Thread.Sleep(1000) in a loop, raising the ReportProgress at each cycle through the loop?

Is that a good idea? It seems to me to be the easiest, certainly the quickest way to achieve it, but is it really good practice? Thread.Sleep() always strikes me as being a bit of a hack, but it works.

I'm sure I'll work something out eventually. Who knows. Could be I'm doing it a good way. Doubt it, though.

Just wanted to share my bafflement. Of course, if anyone knows the answer, then let me know.

{ 0 comments... read them below or add one }

Post a Comment