Windows Forms Visual C ++ 2010-while循环导致崩溃? [英] Windows Forms Visual C++ 2010 - while loop causes crash?

查看:69
本文介绍了Windows Forms Visual C ++ 2010-while循环导致崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是C ++的新手.

我正在编写一个程序,以通过串行端口与设备进行通信.

单选按钮用于切换模式,程序应执行循环,直到单选按钮被更改为止.按下按钮切换到Pmode后,程序将崩溃,无法进行任何操作.

我一直在网上寻找,有类似问题的人建议使用Invoking,但我不太了解如何使用它?

这是相关的代码位:感谢您的任何建议

First of all, I''m pretty new to C++.

I''m writing a program to communicate with a device via the serial port.

Radio buttons are used to toggle the mode and the program is supposed to execute a loop until a radio button is changed. As soon as the button is pressed to toggle onto Pmode, the program crashes and nothing can be pressed.

I''ve been looking online and people with similar problems have suggested using Invoking but I don''t really understand how to use this?

Here''s the relevant bit of code: thanks for any advice

private: System::Void rbCMode_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
{
  if(rbCMode->Checked)
  {
    //set to command mode
    String^ message = "Set_Mode_C";
    mySerialPort->WriteLine(String::Format("{0}", message));

    //wait for OK confirmation
    OKCheck();
  }
  else //Pmode must be checked
  {
    //set to polling mode mode
    String^ message = "Set_Mode_P";
    mySerialPort->WriteLine(String::Format("{0}", message));
    // wait for OK confirmation
    OKCheck();
  }
             
  while(rbPMode->Checked == true)
  {   
    ReadAngles(xangle, yangle, x_angle_text, y_angle_text); //read the angles
    txtXDisplay->Text=x_angle_text; //update displays
    txtYDisplay->Text=y_angle_text;
  }
}

 

static void ReadAngles(double^ %xangle, double^ %yangle, String^ %x_angle_text, String^ %y_angle_text)
{
  //read from the buffer
  //check the number of bytes in buffer, dont read unless 19 bytes
  int databytes = mySerialPort->BytesToRead;
  while (databytes < 19) 
  {
    databytes = mySerialPort->BytesToRead;
  }

  //if the received string is "OK" then reject
  String^ buffer = mySerialPort->ReadLine();
  while (buffer->Length <= 5)
  {
    buffer = mySerialPort->ReadLine();
  }

  //split string into correct parts and convert to double
  x_angle_text = buffer->Substring(2,6);
  y_angle_text = buffer->Substring(11,6);
  xangle = System::Convert::ToDouble(x_angle_text);
  yangle = System::Convert::ToDouble(y_angle_text);
  return;
}

推荐答案

您永远不会从事件处理程序中返回.因此,发生的崩溃并不是真正的崩溃,而是挂断,因为GUI无法再响应事件.进入rbCMode复选框的事件处理程序后,您将立即进入GUI thread.只要事件处理程序不返回,就不会发生其他GUI事件.您需要从一个单独的线程中的while循环开始该部分.请注意,尽管不能直接从非GUI线程访问GUI组件.您必须为此使用Invoke.

干杯!

—MRB
You are never returning from the event handler. So it''s not really a crash that happens, but rather a hangup because the GUI can no longer respond to events. As soon as you the enter the rbCMode checkboxe''s event handler you are on the GUI thread. As long as the event handler does not return no other GUI events can take place. You need to start the part from the while loop in a separate thread. Be carefull though that you can''t access GUI components directly from a non-GUI thread. You''ll have to use Invoke for that.

Cheers!

—MRB


这篇关于Windows Forms Visual C ++ 2010-while循环导致崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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