gpt4 book ai didi

c++ - 托管 C++/CLI .net 使用线程更新来自不同类的 GUI 组件/文本框

转载 作者:行者123 更新时间:2023-11-28 03:25:00 25 4
gpt4 key购买 nike

我在从多个类更新 UI 组件时遇到问题。我已经宣布了两个类(class)。第一个是 ClassMain,它包含一个 GUI/UI 文本框。我还声明了一个名为 ClassTwo 的第二类。在主类中声明了 ClassTwo 的实例。

为了使场景更加复杂,我在等式中添加了线程。众所周知,线程很有用,因为它们可以防止 GUI 锁定并进一步提高 CPU 吞吐量。我所追求的是一种安全地更新两个类的文本框的解决方案,该文本框也是线程安全的。目前我不知道如何从 ClassTwo 访问 textBox1,所以我也很想看到解决方案。我在下面附上了我的代码(没有文本框更新,因为我不确定如何执行此操作)。
感谢任何帮助。谢谢。

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;

ref class ClassTwo
{
public:
ClassTwo(void);
void DoProcessing(void);
};

public ref class ClassMain : public System::Windows::Forms::Form
{
public: //Constructor of Main Class
ClassMain(void)
{
InitializeComponent();
backgroundWorker1->RunWorkerAsync();
backgroundWorker2->RunWorkerAsync();
}

protected:
~ClassMain() //Deconstructor of main class
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
private: System::ComponentModel::Container ^components;
//Decleare 2 background Worker threads to perform our calculation and logicwork
//One will execute work through through ClassMain the other using ClassTwo's method of DoProcessing
private: System::ComponentModel::BackgroundWorker^ backgroundWorker1;
private: System::ComponentModel::BackgroundWorker^ backgroundWorker2;
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e );
void backgroundWorker2_DoWork( Object^ sender, DoWorkEventArgs^ e );

//Declare an instance of Class Two
private: ClassTwo^ myclass2;


void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
this->textBox1->Location = System::Drawing::Point(42, 61);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(409, 71);
this->textBox1->TabIndex = 0;

this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &ClassMain::backgroundWorker1_DoWork);

this->backgroundWorker2 = (gcnew System::ComponentModel::BackgroundWorker());
this->backgroundWorker2->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &ClassMain::backgroundWorker2_DoWork);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(507, 189);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
};

void ClassMain::backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
this->myclass2->DoProcessing();
}

void ClassMain::backgroundWorker2_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
int j;
for (j=0;j<10000;j++)
{
//Write the output to our textbox backgroundWorker1
//this->textBox1->AppendText("Hello From ClassMain: The Value of j is" + j.ToString() + "\r\n");
}
}

//Constructor of ClassTwo
ClassTwo::ClassTwo(void)
{
}
//DoProcessing of ClassTwo
void ClassTwo::DoProcessing(void)
{
int i;
for (i=0;i<10000;i++)
{
//Write the output from ClassTwo to our common textbox from backgroundWorker2
//this->textBox1->AppendText("Hello From Class 2: The Value of i is" + i.ToString() + "\r\n");
}
}


[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew ClassMain());
return 0;
}

最佳答案

调用textBox1->BeginInvoke(...)textBox1->Invoke(...)。 BeginInvoke/Invoke 是 Control 类的方法,所有控件都继承自该类。

关于c++ - 托管 C++/CLI .net 使用线程更新来自不同类的 GUI 组件/文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com