c#嵌套线程

nested Threads with c#

嗨,我对一般线程非常陌生。

我正在用 C# 开发类似蓝牙聊天的东西。

主要形式有 2 种可能性

1.更新BT设备列表并连接设备(工作正常)

2.监听连接到自己BT芯片的设备。

用于测试BT功能被禁用。

对于#2,我在加载表单后创建了一个线程。

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

因为监听总是正确的,所以创建并显示了一个新的表单(新的 sendReceiveForm)。

新表单包含另一个线程,该线程在新表单加载后执行

并有一个循环检查是否收到信息。

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

第二种形式的流是来自一个简单的.txt streamReader 的流,具有1 位数字。

第二个线程陷入无限循环(或看起来如此)并且应用程序崩溃。

我不知道问题出在哪里,也不知道如何解决。

//编辑

将方法更新为:

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

但没有区别

//EDIT2

问题是它工作正常,直到启动 2 线程。

如果我评论 2 线程的开头,则创建表单并且似乎工作正常。


我很确定这个子句隐藏了这个问题:

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

删除它,因为它会捕获所有异常并且什么都不做。删除后再次以调试模式运行,您将看到实际错误。看看这里。


该表单可能没有正确创建,因为您的后台线程中充斥着消息,而后台线程总是在一个紧密的无限循环中执行。

为什么不尝试使用"阻塞"流,以便它阻止线程执行,直到通过蓝牙接收到实际数据


所以 Stream.ReadByte() 说:

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

一旦网络流关闭,它就会保持这种状态。所以溪流说:我关门了。但是你的代码继续循环,得到-1,循环得到-1,循环。你需要做例如:

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

请注意,它修复了代码示例中的第二个问题...它执行 ReadByte 并将值丢弃,然后将下一个字节发送到屏幕。

上面的吉姆米歇尔已经说过了,但你似乎错过了他的答案。 :-)

接受

无论如何,有一个更早的问题......"流"被初始化为什么?假设您正在使用来自 32feet.NET 的 BluetoothListener(它使用相同的模式以及 FCL 的 TcpListener 和 IrDAListener),您需要接受一个新连接。你在某个地方这样做吗?我希望 checkForConnection 这样做:

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

你的 receiveData 方法很奇怪。

所以它是在非 UI 线程上调用的。假设它读取的字节不是 -1。

调用是必需的,所以它会回调自己。

现在它读取一个字节并得到一个-1(你说文件只有1位)。

现在它永远在循环中旋转。

此外,如果您在 UI 线程上调用该方法并且它读取一个字节,则它必须在输出之前读取另一个字节。

如果您正在查看尚未关闭的网络流,这也可能在第二次调用 stream.ReadByte 时挂起。在流关闭之前,您的代码无法知道已经到达终点。这与文件不同。

你还没有创造出逃避循环的方法。此方法中没有任何内容将 listening 设置为 false。由于它在 UI 线程上旋转,因此您无法按下取消按钮或其他任何东西。

我想你想做的是:

private void MainForm_Load(object sender, EventArgs e)

  {      

    incomingConnectionThread = new Thread(checkForConnection);

    incomingConnectionThread.Start();

  }



  public void checkForConnection()

  {

    while (listening) //boolean which is always true

    {

      if (cargui == null)

      {

        cargui = new sendReceiveForm(null);

        cargui.ShowDialog();

      }

    }

  }
  private void CarGui_Load(object sender, EventArgs e)

  {

    thread = new Thread(receiveData);

    thread.Start();

  }



  private void receiveData()

  {

    while (listening)

    {

      try

      {

        if (stream.ReadByte() != -1)

        {

          if (rtfReceiveWindow.InvokeRequired)

          {

            rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

          }



          else

          {

            rtfReceiveWindow.Text += stream.ReadByte().ToString();

          }

        }         

      }

      catch

      {

      }



    }

  }

private void receiveData()

  {

    while (listening)

    {        

      if (stream.ReadByte() != -1)

      {

        if (rtfReceiveWindow.InvokeRequired)

        {

          rtfReceiveWindow.Invoke(new rtfDelegate(receiveData));

        }



        else

        {

          rtfReceiveWindow.Text += stream.ReadByte().ToString();

        }

      }         

    }

  }
  catch

  {

  }

void DataReader_Runner(object state)

{

 Stream strm = (Stream)state;

 while (true) {

  int bi = strm.ReadByte(); // This blocks waiting for data...

  if (bi == -1)

   break;

  byte b = (byte)bi; // Now we know that it is not the special -1 int value

  ... use the new byte ...

 }

}

while (!appQuitting) {

 // This blocks waiting for a incoming connection...

 var conn = listener.AcceptBluetoothClient();

 var strm = conn.GetStream();

 ThreadPool.QueueUserWorkItem(DataRead_Runner, strm);

}

private void updateReceived(string s)

{

  if (rtfReceiveWindow.InvokeRequired)

  {

    rtfReceiveWindow.Invoke(new Action(() => updateReceived(s); }));

  }

  rtfReceiveWindow.Text += s;

}



private void receiveData()

{

  while (listening)

  {

    int recv = stream.ReadByte();

    if (recv != -1)

    {

      updateReceived(recv.ToString());

    }

  }

}

相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161