0

複数のクラスから UI コンポーネントを更新できません。2 つのクラスを宣言しました。1 つ目は、GUI/UI テキストボックスを含む ClassMain です。ClassTwo という 2 番目のクラスも宣言しました。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

新しい 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 に答える
1

textBox1- >BeginInvoke(...)またはtextBox1->Invoke(...)を呼び出します。BeginInvoke/Invoke は、すべてのコントロールが継承する Control クラスのメソッドです。

于 2013-01-16T07:14:53.947 に答える