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

用什么来代替Suspend和Resume
Suspend和Resume 在VS2008中调用会提示 已经过时,现在公司要求编译时不允许出现警告,而以前的代码组织比较有问题,请见下面代码:
public void PauseThread(int ThreadNum)
  {
  for (int i = 0; i < ThreadNum; i++)
  {
  if (mThreadPool[i].ThreadState != ThreadState.Stopped)
  {
  mThreadPool[i].Suspend(); 
  }
  }
  mIsAllThreadPause = true;
  }
public void ContinueThread(int ThreadNum)
  {
  for (int i = 0; i < mThreadPool.Length; i++)
  {
  if (mThreadPool[i] != null && mThreadPool[i].ThreadState != ThreadState.Stopped)
  {
  mThreadPool[i].Resume();
  }
  }
  }



 public void RunThread(TabPage GenPage, GroupBox CtrlBox, TextBox ScenarioBox, TextBox LogBox, LogWriter pLog)
  {
  for (int i = 0; i < NumofThread; i++)
  {
  ......
  mThreadPool[i] = new Thread(new ParameterizedThreadStart(ThreadPro));
  bool TimeFlag = false;
  do
  {
  TimeFlag = true;
  if (Time <= DateTime.Now.Ticks)
  {
  TimeFlag = false;
  }
  } while (TimeFlag);
  mThreadPool[i].IsBackground = true;
  mThreadPool[i].Start(pParam);
  }
  }


有什么办法可以让以上代码编译时没警告? 本人调查下来可以用类似Mutex 的WaitOne方法,不过这样的话在TREADSTART方法里比较适合用循环的方法去实现,不过我的TREADSTART方法中没有循环

------解决方案--------------------
给搂主写了一个演示代码,搂主可以体验一下使用控制变量控制线程的方式:
C# code
public partial class MainForm : Form
{
    public MainForm()
    {
        this.InitializeComponent();
    }

    private Thread m_thread = null;
    private ThreadState m_state = ThreadState.Unstarted;

    private void HandleTask()
    {
        this.m_state = ThreadState.Running;
        int i = 0;
        while (i < 100000)
        {
            //已经暂停
             if(this.m_state == ThreadState.Suspended)
                continue;
            //请求暂停
              if (this.m_state == ThreadState.SuspendRequested)
            {
                this.m_state = ThreadState.Suspended;
                continue;
            }

            //已经停止
             if (this.m_state == ThreadState.Stopped)
                break;
            //请求停止
             if (this.m_state == ThreadState.StopRequested)
            {
                this.m_state = ThreadState.Stopped;
                break;
            }

            //处理主要任务,这里休息100ms是为了模拟实际任务的执行时间
            Thread.Sleep(100);
            i = i + 1;
            
            //在界面上显示信息
            this.Invoke(new ShowInfoHandler(this.ShowInfo), new object[] { i });
        }
        this.m_thread = null;
        this.m_state = ThreadState.Stopped;
    }

    public void Start()
    {
        if (this.m_thread == null)
        {
            this.m_thread = new Thread(new ThreadStart(this.HandleTask));
            this.m_thread.Start();
        }
        if (this.m_state == ThreadState.Suspended)
            this.m_state = ThreadState.Running;
    }

    public void Pause()
    {
        this.m_state = ThreadState.SuspendRequested;
        while (this.m_state != ThreadState.Suspended)
            Application.DoEvents();
    }

    public void Stop()
    {
        this.m_state = ThreadState.StopRequested;
        while (this.m_state != Th