user758362
user758362

Reputation: 291

An unhandled exception of type 'System.Reflection.TargetParameterCountException'

I created a BeginInvoke so I could write to a text box from a non-UI thread. Thread A calls a delegate which runs testFunc in thread A's context. testFunc then does a BeginInvoke which runs the empty function ControlBoxDelegateMethod. If the BeginInvoke line is removed, the program runs. But if it is left in, I get the following exception:

An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll Additional information: Parameter count mismatch.

        private:
        //delegate void ControlBoxDelegate(Label^ myControl,int whichControl);
        void ControlBoxDelegateMethod(Label^ myControl,int whichControl)
        {
         //  myControl->Text = "Test!!!!!!!";
        }
        public: 

        void testFunc()
        {
            int which = 3;
            local_long_textBox->BeginInvoke(gcnew  ControlBoxDelegate
                           (this,&Form1::ControlBoxDelegateMethod),which);
        }

Could anyone shed some light on what I am doing wrong here? Thanks!

Upvotes: 3

Views: 6886

Answers (1)

Etienne de Martel
Etienne de Martel

Reputation: 36984

ControlBoxDelegateMethod takes two parameters (a Label^ and an int), but you're only passing one (an int named which). You're missing the first parameter.

So, it should probably go like this:

local_long_textBox->BeginInvoke(gcnew ControlBoxDelegate(this,&Form1::ControlBoxDelegateMethod), your_label, which);

Upvotes: 3

Related Questions