正在尝试从窗体运行线程
Trying to run a thread from a form
我对下面的代码有两个相关的问题
1) 我正试图从Visual C++窗体的串行端口读取。我想在InitializeComponent函数中创建一个线程,但当我包含启动线程的调用时,在窗体页面上出现了此错误:
警告1找不到类型"Thread"。请确保引用了包含此类型的程序集。如果此类型是您的开发项目的一部分,请确保该项目已成功生成。
2) 线程将在静态函数Read中运行。Read需要解析主形式中的串行端口(串行端口名为arduino),但它显然无法解决这些问题:"ReadLine的左边必须有类/结构/联合"
建议?
using namespace System::IO::Ports;
using namespace System::Threading;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
private: void static Read(void)
{
while (1)
{
try
{
String^ message = arduino.ReadLine();
// this->ArduinoOutputTextBox->Text = message;
}
catch (TimeoutException ^) { }
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ USB_button;
private: System::IO::Ports::SerialPort^ arduino;
private: System::Windows::Forms::TextBox^ ArduinoOutputTextBox;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
Thread^ readThread = gcnew Thread(gcnew ThreadStart(Read));
this->components = (gcnew System::ComponentModel::Container());
this->USB_button = (gcnew System::Windows::Forms::Button());
this->arduino = (gcnew System::IO::Ports::SerialPort(this->components));
this->ArduinoOutputTextBox = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
arduino
是对对象的引用,而不是实际对象。
你必须写arduino->readLine()
。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 如何在C++生成器中运行两个窗体
- 可视C++窗体应用程序无法在其他计算机上运行
- 如何在visual studio 2010中使用窗体应用程序按钮来运行控制台文件
- 正在尝试从窗体运行线程
- 在使用相同C++代码的跨平台窗体之间获取运行时错误