Caner
Caner

Reputation: 59338

Difficulty in understanding c# method description

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?

Upvotes: 0

Views: 113

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1064324

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.

Upvotes: 6

Bas
Bas

Reputation: 27115

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.

Upvotes: 1

Oded
Oded

Reputation: 499402

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.

Upvotes: 1

Related Questions