-1
delegate void ReportCreatorHandler();
void ReportCreator(Report report, bool isExport, bool isPrint)
{
            if (this.InvokeRequired)
            {
                this.Invoke(new ReportCreatorHandler(delegate() { ReportCreator(report, isExport, isPrint); }));
            }
            else
            {
            ------;
            ------;
            TForm reportPage = new TForm();
            ------;
            ------;
            this.tabpages.add(tabpage1);//tabpages is TabPageCollection
            }
}

Eventhough i handled "InvokeRequired" in the above code,while adding a tabpage to a TabPageCollection,i am getting an error "Controls created on one thread cannot be parented to a control on a different thread".Also i tried accessing InvokeRequired object on TabPageCollection,property is not there for that collection.Can anyone help in handling the above exception ?? Thanks in advance.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – slugster Feb 16 '15 at 06:38
  • Also see: [Automating the InvokeRequired code pattern](http://stackoverflow.com/q/2367718/109702). – slugster Feb 16 '15 at 06:38
  • 1
    You're messing up with creating controls in different thread. Don't do that. – Sriram Sakthivel Feb 16 '15 at 06:42

1 Answers1

0

There is one case when the InvokeRequired property return false, although you are on a different thread. -> in case the handle of the connected control is not created yet, or already disposed.

-> https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx

so you either check the ishandledcreated property as well, or you switch to SynchronizationContext for marshalling. Which is recommended.

Martin Moser
  • 6,219
  • 1
  • 27
  • 41