0

I was looking at Control.BeginInvoke method and I didn't quite get what it means when it says:

Executes a delegate asynchronously on the thread that the control's underlying handle was created on.

What is control's underlying handle? What does it do?

Caner
  • 49,709
  • 33
  • 153
  • 169

3 Answers3

6

Controls have "thread affinity" - meaning there is a demand that they are only directly manipulated (or even inspected, except a few specific properties such as InvokeRequired) by the thread that created them (commonly called the UI thread). The handle is simply the abstraction between the OS control and the .NET control.

What this actually does is place a message on the windows message loop, that is picked up by the UI thread (which owns the control), causing your delegate to be invoked on the UI thread. This means it is allowed to talk to the control. This is useful if you are currently on a background thread (maybe an async callback or BackgroundWorker), and need to update the UI.

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
1

It means the windows handle - the internal, non managed reference to the control.

See Contorl.Handle:

Gets the window handle that the control is bound to.

And:

The value of the Handle property is a Windows HWND.

Oded
  • 463,167
  • 92
  • 837
  • 979
1

Windows Forms controls are created on a specific thread, and are not designed for use in multithreaded environments. Microsoft made it so that the control can only be manipulated from the thread it was created on, thus forcing a single threaded environment on the controls.

BeginInvoke on a control invokes the code supplied to it on that thread.

Bas
  • 25,270
  • 7
  • 45
  • 82