Peter H
Peter H

Reputation: 901

Managed C++/CLI .net Update GUI components /textbox from different classes with threading

I'm having trouble update UI components from multiple classes. I have declared two classes. The first is ClassMain which contains a GUI/UI textbox. I have also declared a second class called ClassTwo. An instance of ClassTwo is declared in the main class.

To complicate the scenario further I have added threading into the equation. As you all know threads are useful because they prevent the GUI from locking up and further enhance CPU throughput. What I am after is a solution to safely update the textbox from both classes that is also threadsafe. Currently I don't know how to access the textBox1 from ClassTwo so I'm also keen to see a solution to this. I have attached my code below (without the textbox updates as I'm unsure of how to do this).
Any help appreciate. Thanks.

#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;
}

Upvotes: 0

Views: 2965

Answers (2)

doug65536
doug65536

Reputation: 6781

Make a new windows forms application and paste this in right below Form1 constructor.

    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);
    }

It demonstrates using BeginInvoke to run a delegate on the GUI thread.

Upvotes: 1

user1610015
user1610015

Reputation: 6678

Call textBox1->BeginInvoke(...) or textBox1->Invoke(...). BeginInvoke/Invoke are methods of the Control class, which all controls inherit from.

Upvotes: 1

Related Questions