I cannot understand what the below code do

What is the InvokeRequired do?
The delegate I do not know much about delegates

if (lblError.InvokeRequired)
                  lblError.Invoke(new MethodInvoker(delegate { 
                                                                lblError.Text = "License management. Information on active license, renewing license and acquiring new ones. Commercial for limited computers"; 
                                                                picUpdatingFormat.Visible = false; 

Recommended Answers

All 3 Replies

As a simple explanation; it requests that the code you provide is executed on the thread that created the controls owner. This is typically the UI thread.

"Ketsuekiame" Is this the work of InvokeRequired property? it requests that the code I provide within the delegate to be executed on the thread on which the control is created or you mean that all the above code is consider that request?

One of the things about C# and .NET is that you can only update controls on what is known as the UI (User Interface) thread. If you are using multi-threading you might want to change a control from another thread and this is where Invoke and InvokeRequired come in handy.

If you are not on the UI thread, then InvokeRequired will return true. This means you'll have to call the Invoke method on the control, passing it a delegate to a method that will do the actual update.

In your code, they haven't created a delegate for the Invoke, so they create an anonymous delegate of an anonymous method. That's what all the new MethodInvoke ... stuff is doing.

Now if InvokeRequired is false you can change the control directly (you'll often see this in the else portion of an if (blah.InvokeRequired) statement).

commented: Helping explanation! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.