简体   繁体   中英

How to pass reference to Winform member function to different thread different class?

I am trying to update a Winform picturebox from another class running on another thread. I followed the answer at Update WinForm Controls from another thread _and_ class which appears to have all the information except how, exactly, to pass the reference to the Winform/Winform member function to the thread in the constructor/how to call the Winform member function from the thread.

My code is as follows:

//Camera.h

public class CameraThread
{
    public:
       CameraThread(????)
       {
           // constructor
       }

       void DoSomething
       {
         // call UpdateBox on Form1
         ????
       }
};

//Form1.h
namespace SV7 {

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

    public ref class Form1 : public System::Windows::Forms::Form
    {
        private: CameraThread* CT;
        private: boost::thread* ImageACQ;

        public: delegate void UpdateDelegate(void);
        public:
            Form1(void)
            {
                CT = new CameraThread(????);
                boost::thread* ImageACQ = new boost::thread(&CameraThread::ImageAcquisition,CT);
                InitializeComponent();
                InitializeCanny();
            }

            void UpdateBox(void) 
            {
                if (this->RawImageBox->InvokeRequired) 
                {
                    UpdateDelegate^ d = gcnew UpdateDelegate(this,&Form1::UpdateBox);
                    this->RawImageBox->BeginInvoke(d);
                }
                else 
                {
                }
            }
    };
}

Where I have put the ????, I do not know what syntax to use to pass/call the member function. I assume that a forward declaration of Form1 is needed in my Camera.h but was having trouble referencing the actual form class in Form.h. However, if a straight function pointer was used, then I do not think that would be the way to go.

By way of background, I am using VS2010 SE C++. I am trying to have an separate thread that grabs images from a webcam, processed them using OpenCV, and then displays the results in a picturebox on the GUI thread.

I am just ignorant of how to do this and would appreciate any guidance. Thanks in advance/

You need your Form1 instance inside the class, so your constructor call should look like this:

CT = new CameraThread(this);

I'm not that much into C++/CLI so maybe it's not perfect syntax but the meaning should be clear. Your class has to take the instance of Form1 and save it until it's needed to call UpdateBox:

public class CameraThread
{
    private:
       Form1^ form;
    public:
       CameraThread(Form1^ form)
       {
           // constructor
           this->form = form;
       }

       void DoSomething
       {
         // call UpdateBox on Form1
         form->UpdateBox();
       }
};

Please note that you are mixing two powerful libraries, the .NET Framework and boost. If you don't need them both you should stick with one of them. The .NET Framework has threads too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM