Thread.Sleep()

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

I have a running thread and inside the Run() Method
I have a little timer which uses Thread.Sleep like so:

for (int i=interval; i>0; i--)
{
timerLbl.Text=i.ToString();
Thread.Sleep(1000);
}

Now this works fine with the full .NET framework but
on the Compact Framework it just freezes my program.

Why doesn't this work on the CF?

Drew
 
It's not a very good idea to put an interface thread to sleep, but if you
insist, insert Application.DoEvents() before call to ThreadSleep()
 
Hmmm...

I don't want to put the User Interface thread to sleep, just the thread I
started.

How do I specify this?

Drew
 
I just inserted an Application.DoEvents() like so:

for (int i=interval; i>0; i--)
{
timerLbl.Text=i.ToString();
Application.DoEvents();
Thread.Sleep(1000);
}

But it still freezes on the PocketPC (works OK on full .NET framework)

When I removed the timerLbl.Text=i.ToString() it worked on the PocketPC.

However, this is NOT the solution I was after.

Drew
 
Hi,

Why are you doing this? Wouldn't it make sense to simply use a timer?

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Oh, wait... Are you doing this in a separate thread? In that case you
problem is that you are trying to modify control properties in a non-GUI
thread.
You need to use Control.Invoke instead

string labelText;

private void SetStatus(object sender, EventArgs e)
{
timerLbl.Text = labelText;
}


for (int i=interval; i>0; i--)
{
labelText=i.ToString();
timerLbl.Invoke(new EventHandler(SetStatus));
Thread.Sleep(1000);
}
 
This works - thanks!

Can I also send an arugument to the SetStatus method?
e.g. setStatus(i);

Regards,

Drew
 
Unfortunately, no.
This is IMO an unreasonable shortcoming of CF, something we have to live
with for now.
 
Hi,

Well, you mention a separate thread. And, Alex pointed you at the Invoke
which is needed to marshal data to an STAThread.

However, this sort of thing (still) is done with a Timer -- in a form, using
a Timer control, or in a thread using System.Threading.Timer. If you have
a separate thread, you still need to use Invoke.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)


See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Hmmm... I haven't really used System.Threading.Timer yet.

I assume that what you do is create a new timer and pass it
some arguments (like intervals) and then it generates an event
which is received in a certain method (?) every x seconds?

Are there advantages to using this over the quick hack I made up?

Drew
 
Hi,

The basic advantage is that Framework code uses underlying Windows timer
mechanisms, which are tied to hardware.

Using System.Threading.Timer, you create a timeout when you call the
constructor (an argument). The delegate that you also pass as an argument
to the constructor is called when the timeout occurs. There is a little more
to it than this, but it is pretty straight forward.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Back
Top