Pausing a Thread
I am u开发者_StackOverflow社区sing following code to create and execute thread i need to stop it but cant see thread .Sleep
any ideas how can i stop for a while the execution there?
var t = new Thread(() =>
{
try
{
//Line 1
Pause here
//Line 2
Pause here
//Line 3
}
catch (Exception ca)
{
MessageBox.Show(ca.Message);
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
If you mean that you want to instruct the Thread to wait at a particular checkpoint until some other Thread tells it to continue, one good option is to use AutoResetEvent.
t.SetApartmentState(ApartmentState.STA);
I'll work from the assumption that this was intentional. Couple of things you have to do to meet the STA contract. You have to pump a message loop, call Application.Run(). And you cannot pause, that blocks any marshaled call and makes deadlock very likely.
Not a problem, when you pump a message loop you're always in an idle 'pause' state. Make code run just as you'd normally do in a GUI main thread. Application.ExitThread ends the thread.
Use the static method Thread.Sleep(int)
inside your delegate
http://msdn.microsoft.com/en-us/library/d00bd51t.aspx
Suspends the current thread for a specified time.
Are you looking for the System.Threading.Thread.Sleep()
method?
add this line at the top of your code:
using System.Threading;
then you can see Thread.Sleep()
精彩评论