0

My code shows the following error message before compiling

"An object reference is required"

the wrong code is:

Control.Invoke(new invokeDelegate(invokeMethod));

I put a static before invokeMethod instance but it doesn't work... Does anyone know how to fix this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Thread invokeThread;
        private delegate void invokeDelegate();
        private void StartMethod()
        {
            //Code C
            Control.Invoke(new invokeDelegate(invokeMethod));
            //Code D
        }
        private void invokeMethod()
        {
            //Code E
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Code A
            invokeThread = new Thread(new ThreadStart(StartMethod));
            invokeThread.Start();
            //Code B
        }  
    }
}
MatSnow
  • 7,357
  • 3
  • 19
  • 31
DaveG
  • 491
  • 1
  • 6
  • 19

1 Answers1

0

The problem is that you are calling Invoke on the Control class itself. You need to call Invoke on an instance of a control.

Have a look at the example code at the bottom of https://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.110).aspx, where the code calls myFormControl1.Invoke.

Reg Edit
  • 6,719
  • 1
  • 35
  • 46