线程正在退出

本文关键字:退出 线程 | 更新日期: 2023-09-27 18:21:57

我有一个线程的小问题。我在超时设置为600的情况下执行接收功能,但它在这里结束

if ((thr.ThreadState != ThreadState.Running) 
    && (thr.ThreadState != ThreadState.WaitSleepJoin))
{
  ThreadState st = thr.ThreadState;
  lg.WriteString("EXIT BY THREAD END");
  lg.WriteString("LAST: "+st.ToString());
  break;
}

只有当线程结束并且我已经收到所有数据时,函数才应该在那里结束,但当我打开errors.txt文件时,我看到ThreadState值是Running或WaitSleepJoin。

知道为什么函数要中止吗?

下面的更多代码->

bool exit = false;
void WaitAndRead() {
if (buffer == null)
  buffer = new List<byte>();
buffer.Clear();
try
{
  while (sock_client.Available < 1 && !exit)
    Thread.Sleep(10);
  while (sock_client.Available > 0 && !exit)
  {
    byte[] b = new byte[sock_client.Available];
    sock_client.Receive(b, 0, b.Length, SocketFlags.None);
    buffer.AddRange(b);
    Thread.Sleep(25);
  }
}
catch (Exception ex) 
{ 
  Log lg = new Log("C:''TPV''errores.txt"); 
  lg.WriteException(ex, true); 
}
public int Receive(int timeout,out List<byte> result) 
{
  result = new List<byte>();
  try
  {
    if (!Connected)
      return NOT_CONNECTED;
    bool ok = true;
    ThreadStart str = delegate 
    {
      try { WaitAndRead(); }
      catch (Exception ex) 
      { 
        Log lg = new Log("C:''TPV''errores.txt"); 
        lg.WriteException(ex, true); ok = false; }
      };
      exit = false;
      Thread thr = new Thread(str);
      thr.Start();
      int x = 0;
      int max = 10 * timeout;
      while (x < max)
      {
        Log lg = new Log("C:''TPV''errores.txt");
        if ((thr.ThreadState != ThreadState.Running) 
            && (thr.ThreadState != ThreadState.WaitSleepJoin))
        {
          ThreadState st = thr.ThreadState;
          lg.WriteString("EXIT BY THREAD END");
          lg.WriteString("LAST: "+st.ToString());
          break;
        }
        x++;
        Thread.Sleep(10);
      }
      exit = true;
      if (x >= max)
        return Timeout;
      result = new List<byte>(buffer.ToArray());
      return ok?OK:NOT_CONNECTED;
    }
    catch(Exception ex) 
    {
      Log lg = new Log("C:''TPV''errores.txt"); 
      lg.WriteException(ex, true);
      return NOT_CONNECTED;
    }
  }

线程正在退出

在评估此代码时,ThreadState属性的值可能正在更改:

if ((thr.ThreadState != ThreadState.Running) && (thr.ThreadState != ThreadState.WaitSleepJoin))
{
    ThreadState st = thr.ThreadState;
    lg.WriteString("EXIT BY THREAD END");
    lg.WriteString("LAST: "+st.ToString());
    break;
}

考虑以下场景:

  1. thr.ThreadState就是WaitSleepJoin
  2. if语句的第一部分求值为true
  3. 在评估if的第二部分之前,thr.ThreadState变为Running
  4. if语句的第二部分求值为true
  5. 由于两个部分都评估为true,因此进入if

您可以尝试如下所示重写代码,以便只检查ThreadState属性的值一次:

var state = thr.ThreadState;
if (state != ThreadState.Running && state != ThreadState.WaitSleepJoin)
{
    lg.WriteString("EXIT BY THREAD END");
    lg.WriteString("LAST: "+ state.ToString());
    break;
}

我注意到Microsoft对ThreadState属性的一些注释:
http://msdn.microsoft.com/en-us/library/system.threading.thread.threadstate.aspx

特别指出:

线程状态只在调试场景中感兴趣。您的代码永远不应该使用线程状态来同步螺纹。

您想转而使用Monitor或Mutex对象吗?