0

我无法从多个类更新 UI 组件。我已经宣布了两个班级。第一个是 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;
}
4

2 回答 2

1

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

于 2013-01-16T07:14:53.947 回答
1

创建一个新的 windows 窗体应用程序并将其粘贴到 Form1 构造函数的正下方。

    public void InvokeSafely(Control control, Action action)
    {
        if (control.InvokeRequired)
            control.BeginInvoke(action);
        else
            action();
    }

    public void RunsInAnotherThread(object dummy)
    {
        InvokeSafely(this, () => Text = "I made the title change safely");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(RunsInAnotherThread);
    }

它演示了使用 BeginInvoke 在 GUI 线程上运行委托。

于 2013-01-17T07:49:50.423 回答