0

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/

Community
  • 1
  • 1
user1805103
  • 127
  • 2
  • 19
  • You should probably make clear upfront that you're using C++/CLI instead of (ISO) C++: it'll help you find the right audience for your question. – Stephen Lin Mar 03 '13 at 07:36
  • Thanks. In response to your comment, I did some reading and you are absolutely correct. I see numerous similar comments to noobs like me and this seems to be a recurring issue. Perhaps it is already there, but it might make sense for SO to consider adding something to the question script indicating this to posters to head off ignorant questions like mine. Thanks again. – user1805103 Mar 03 '13 at 15:35

1 Answers1

1

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks. It still does not compile and gives me an error ("syntax error missing ';' before '^') as indicated above. I assume that is because Camera.h is processed before Form1.h and the compiler does not know what 'Form1' is. Putting in a forward declaration ("class Form1;") just changes the error to ("syntax error 'constant'). Thanks again for any help. – user1805103 Mar 03 '13 at 15:57
  • With C++/CLI you may need "*ref* class Form1;" as your forward declaration. – nvoigt Mar 03 '13 at 16:40