C ++表单在while循环

时间:2015-10-16 07:55:11

标签: c++ multithreading forms while-loop bluetooth

我在c ++中遇到了我的表单问题。由于我的while循环,表单崩溃了。需要while循环来检查从外部设备接收的输出。它实际上是一个日志应用程序,它连续打印从设备接收的数据,即实时日志。我使用的是Visual Studio 2013。

我有1个.cpp和1个头文件。头文件仅包括运行表单的方法。在.cpp文件中,执行文件获取和检查的功能在" Connect"按钮事件。我还有一个断开按钮,显然断开了表单和外部设备之间的连接。我通过蓝牙连接它。它可以完美地接收输出并准确打印出打印所需的内容。唯一的问题是,我不能再单击断开连接按钮,因为它会冻结。

这是我的GetBluetoothOutput代码:

void GetBluetoothOutput(HANDLE btSerial)
        {
            std::map<char, std::string> map = {
                { 'A', "Unit is moving forward at high speed." },
                { 'B', "Unit is moving forward at medium speed." },
                { 'C', "Unit is moving forward at low speed." },
                { 'D', "Unit is moving backward." },
                { 'E', "Turn Left." }};

            int previous = 0;
        while (1)
        {
            DWORD dwCommModemStatus;
            BYTE Byte;
            DWORD dwBytesTransferred;
            int retVal;

            SetCommMask(btSerial, EV_RXCHAR); //receive character event
            WaitCommEvent(btSerial, &dwCommModemStatus, 0); //wait for character

            if (dwCommModemStatus & EV_RXCHAR)
            {
                ReadFile(btSerial, &Byte, 1, &dwBytesTransferred, 0); //read 1
                retVal = Byte;
            }

            if (previous != retVal)
            {
                previous = retVal;
                char *getOutput;
                getOutput = (char*)&retVal;
                auto find = map.find(*getOutput);
                if (find != map.end())
                {
                    std::string description = find->second;
                    m_LogDisplayBox->Items->Add(gcnew String(description.c_str()));
                }
                else
                {
                    m_LogDisplayBox->Items->Add("Unknown input sent from bluetooth.");
                }
            }
            else
            {
                m_LogDisplayBox->Items->Add(".............");
            }
            m_LogDisplayBox->Update();
        }
    }

这是我的Connect按钮代码:

private: System::Void ConnectBtnEvent(System::Object^  sender, System::EventArgs^  e)
{
    m_DisconnectButton->Enabled = true;
    m_ConnectButton->Enabled = false;

    int m_Port = m_PortComboBox->SelectedIndex;
    int m_BaudRate = m_BaudRateComboBox->SelectedIndex;
    int data = m_DataComboBox->SelectedIndex;
    int parity = m_ParityComboBox->SelectedIndex;
    int stop = m_StopComboBox->SelectedIndex;
    int flowControl = m_FlowControlComboBox->SelectedIndex;

    switch (m_BaudRate)
    {
    case 0:
        m_BaudRate = CBR_110;
        break;
    case 1:
        m_BaudRate = CBR_300;
        break;
    case 2:
        m_BaudRate = CBR_600;
        break;
    default:
        m_LogDisplayBox->Items->Add("No \"Baud Rate\" selected.");
    }

    switch (data)
    {
    case 0:
        data = 7;
        break;
    case 1:
        data = 8;
        break;
    default:
        m_LogDisplayBox->Items->Add("No \"Data\" selected.");
    }

    switch (parity)
    {
    case 0:
        parity = NOPARITY;
        break;
    case 1:
        parity = ODDPARITY;
        break;
    case 2:
        parity = EVENPARITY;
        break;
    case 3:
        parity = MARKPARITY;
        break;
    case 4:
        parity = SPACEPARITY;
        break;
    default:
        m_LogDisplayBox->Items->Add("No \"Parity\" selected.");

    }

    switch (stop)
    {
    case 0:
        stop = ONESTOPBIT;
        break;
    case 1:
        stop = ONE5STOPBITS;
        break;
    case 2:
        stop = TWOSTOPBITS;
    default:
        m_LogDisplayBox->Items->Add("No \"Stop\" selected.");
    }

    HANDLE btSerial = BluetoothInit(m_Port, m_BaudRate, data, stop, parity);

    if (isConnected){
        m_LogDisplayBox->Items->Add("Connection Successful!");
        GetBluetoothOutput(btSerial); //This is the method which has the while-loop for continuously reading the outputs from bluetooth.
    }
    else{
        m_LogDisplayBox->Items->Add("Connection Unsuccessful");
        m_DisconnectButton->Enabled = false;
        m_ConnectButton->Enabled = true;
    }

}

我已经读过1解决方案是使用多线程。有没有人可以提供除多线程之外的其他解决方案,因为我对线程不是很熟悉。提前谢谢!

2 个答案:

答案 0 :(得分:0)

看起来你处于一个无限循环中 - 虽然(1)永远不会离开那个循环,你需要做一些类似于While(isConnected)的东西,然后在循环中有一些断开连接的方法。

如果单击断开按钮,可能会检查while循环内部是否有帮助

答案 1 :(得分:0)

尝试使用其他条件。 while (1)是一个无限循环。