日期:2014-05-18  浏览次数:20998 次

如果使纯种处于挂起状态
Resume()和Suspend()这两个方法已经过期了,那么还能用其他的方法吗?使正处于活动的线程挂起,

附代码。

帮忙看看。单击暂停的时候就出现异常。


------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
try
{
if (click == false)
{
yn = ynMethod();
this.btnStart.Text = "暂停游戏";
timer1.Enabled = true;
Thread th;
for (int i = 0; i < arr.Count; i++)
{
th = (Thread)(arr[i]);
if (th.ThreadState == ThreadState.Aborted || th.ThreadState == ThreadState.Stopped)
arr.Remove(th);
}

for (int i = 0; i < arr.Count; i++)
{
th = (Thread)(arr[i]);
if(th.ThreadState==ThreadState.Suspended)
th.Resume();
}
this.timer1.Start();
click = true;

}
else
{
this.btnStart.Text = "开始游戏";
timer1.Enabled = false;
Thread th;
for (int i = 0; i < arr.Count; i++)
{
th = (Thread)(arr[i]);
if (th.ThreadState == ThreadState.Aborted || th.ThreadState == ThreadState.Stopped)
arr.Remove(th);
}
for (int i = 0; i < arr.Count; i++)
{
th = (Thread)(arr[i]);
if (th.ThreadState != ThreadState.Aborted &&th.ThreadState != ThreadState.Stopped)
th.Suspend();
}
this.timer1.Stop();

click = false;
}
}
catch (ThreadStateException ex)
{
MessageBox.Show(ex.Message);
}
}
你没有管理好线程列表,一些线程你已经Abort,但是没有从线程列表中移除
------解决方案--------------------
//单击关闭按钮
private void button1_Click_1(object sender, EventArgs e)
{
if (this.arr.Count==0 )
Application.Exit();
else
{
try
{
Thread th;
for (int i = 0; i < arr.Count; i++)
{
th = (Thread)(arr[i]);
if (th.ThreadState == ThreadState.Suspended)
{
th.Resume();
th.Abort();
}
else
{
th.Abort();
}
}
}
catch (ThreadAbortException ee)
{
}
Application.Exit();
}
}
你在关闭的时候需要判断线程状态,挂起的状态不能直接Abort,否则会抛出异常