当线程处于休眠状态时,为什么我不能使用Pause() [英] Why can't I use the Pause() when the Thread is in the sleeping state

查看:181
本文介绍了当线程处于休眠状态时,为什么我不能使用Pause()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在线程处于休眠状态时使用Pause()?我尝试使用中断方法然后使用Pause()方法,但它在休眠状态下无法工作。请给出我一些提示,谢谢!



这里有一些代码:



Why can't I use the Pause() when the Thread is in the sleeping state?I try to use the Interrupt method and then use the Pause() method,but it can not work during the sleeping state.Please give me some tips,Thank!

Here some code:

private void cmdStart Click(object sender, System.EventArgs e)
{
// Let's create a new thread
primeNumberThread = new Thread(
new ThreadStart(GeneratePrimeNumbers));
 
// Let's give a name for the thread
primeNumberThread.Name = "Prime Numbers Example";
 
primeNumberThread.Priority = ThreadPriority.BelowNormal;
 
// Enable the Pause Button
cmdPause.Enabled = true;
// Disable the Start button
cmdStart.Enabled = false;
 
// Let's start the thread
primeNumberThread.Start();
}
 
private void cmdPause Click(object sender, System.EventArgs e)
{
try
{
try
{
// If current state of thread is Running,
// then pause the Thread
if (primeNumberThread.ThreadState ==
System.Threading.ThreadState.Running)
{
 
//Pause the Thread
primeNumberThread.Suspend();
 
//Disable the Pause button
cmdPause.Enabled = false;
 
//Enable the resume button
cmdResume.Enabled = true;
}
}
catch(ThreadStateException Ex)
{
MessageBox.Show(Ex.ToString(), "Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
} 
}
private void cmdResume Click(object sender, System.EventArgs e)
{
if(primeNumberThread.ThreadState ==
System.Threading.ThreadState.Suspended ||
primeNumberThread.ThreadState ==
System.Threading.ThreadState.SuspendRequested)
{
try
{
// Resume the thread
primeNumberThread.Resume();
 
// Disable the resume button
cmdResume.Enabled = false;
 
// Enable the Pause button
cmdPause.Enabled = true;
}
catch(ThreadStateException Ex)
{
MessageBox.Show(Ex.ToString(), "Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
}
public void GeneratePrimeNumbers()
{
long lngCounter;
long lngNumber;
long lngDivideByCounter;
bool blnIsPrime;
long[] PrimeArray = new long[256];
 
// initialize variables
lngNumber = 3;
lngCounter = 2;
 
// We know that the first prime is 2. Therefore,
// let's add it to the list and start from 3
PrimeArray[1] = 2;
lstPrime.Items.Add(2);
 
while(lngCounter < 256)
{
blnIsPrime = true;
 
// Try dividing this number by any already found prime
// which is smaller then the root of this number.
for(lngDivideByCounter = 1; PrimeArray[lngDivideByCounter]
* PrimeArray[lngDivideByCounter] <= lngNumber;
lngDivideByCounter++)
{
if(lngNumber % PrimeArray[lngDivideByCounter] == 0)
{
// This is not a prime number
blnIsPrime = false;
// Exit the loop break;
}
}
 
// If this is a prime number then display it
if(blnIsPrime)
{
// Guess we found a new prime.
PrimeArray[lngCounter] = lngNumber;
// Increase prime found count.
lngCounter++;
lstPrime.Items.Add(lngNumber);
// Let's put the thread to sleep for 100 milliseconds.
// This will simulate the time lag and we'll get time
// to pause and resume the thread
Thread.Sleep(100);
}
 
// Increment number by two
lngNumber += 2;
}

推荐答案

暂停和<$ c $等方法c>恢复已被弃用,因为这种暂停形式非常危险(顺便说一句,比经常讨论的更危险中止许多人没有正确理解)。如果你需要解释这个危险,我会回答你,但首先要尝试在现有的出版物中找到它。无论如何,永远不要尝试使用暂停/恢复



相反,做一个正确的事情。请参阅我过去的回答:暂停正在运行的主题 [ ^ ]。



-SA
Methods like Pause and Resume are deprecated because this form of pausing is really dangerous (by the way, much more dangerous than frequently discussed Abort which many don't understand correctly). If you need the explanation of this danger, I'll answer you, but first try to find it by yourself in available publications. Anyway, never try to use Pause/Resume.

Instead, do a correct thing. Please see my past answer: pause running thread[^].

—SA


这篇关于当线程处于休眠状态时,为什么我不能使用Pause()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆