Anonymous method in Invoke call
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, an anonymous method is often passed to Invoke when code must run on a specific thread, especially the UI thread in Windows Forms or WPF. The idea is simple: wrap the work in a delegate and let Invoke execute that delegate in the correct context.
Why Invoke needs a delegate
Methods such as Control.Invoke and Dispatcher.Invoke do not run arbitrary statements by themselves. They accept a delegate that describes the work to perform.
An anonymous method is one way to create that delegate inline:
This is useful when the code is short and only needed once. Instead of declaring a separate named method, you create the delegate at the call site.
Common Windows Forms example
In Windows Forms, background work cannot safely update controls directly. If a worker thread needs to change a label or progress bar, the update must be marshaled back to the UI thread.
The anonymous method runs on the UI thread because Invoke schedules it there. Without that Invoke call, touching statusLabel from the background thread would be invalid.
Anonymous method versus lambda
Modern C# often uses a lambda instead of the older delegate syntax:
Both forms create a delegate. The anonymous-method form:
and the lambda form:
usually mean the same thing in this situation. Lambdas are more common today, but understanding anonymous methods still matters because older WinForms and WPF codebases use them heavily.
Returning values from Invoke
Invoke can also execute delegates that return a value. In that case, the delegate type must match the return type.
This is useful when code on a background thread needs to read something from the UI thread and receive the answer synchronously.
Prefer async-friendly patterns when possible
Invoke is synchronous. The calling thread waits until the UI thread runs the delegate. That is sometimes necessary, but it can also block more than you want.
If you only need to queue a UI update and do not need the result immediately, BeginInvoke in WinForms or Dispatcher.BeginInvoke in WPF can be a better fit.
This posts the work to the UI thread and returns immediately.
In newer async code, the cleanest design is often to keep the background work in Task.Run and keep UI updates in the async event handler itself, which can reduce the amount of explicit Invoke code you need.
Captured variables and closures
Anonymous methods can capture variables from the outer scope. That is convenient, but it means the delegate closes over those values.
This behavior is powerful, but be careful when the captured variable changes later or is shared across multiple queued delegates. The code may not behave the way you first expect.
Common Pitfalls
The biggest mistake is assuming Invoke itself makes background work asynchronous. It does not. Invoke is about thread affinity, not parallelism. It simply ensures the delegate runs on the target thread.
Another issue is overusing anonymous methods for large blocks of logic. When the delegate grows beyond a few lines, a named method is usually clearer and easier to debug.
Developers also confuse Invoke and BeginInvoke. Invoke blocks until completion, while BeginInvoke schedules the work and returns immediately. Choosing the wrong one can hurt responsiveness.
Finally, captured variables can surprise you. If the anonymous method relies on outer state that changes before execution, the final UI update may not use the value you thought it would.
Summary
- An anonymous method is an inline delegate that can be passed directly to
Invoke. - This pattern is common when marshaling UI updates back to the correct thread.
- Lambdas are the modern alternative, but the underlying delegate idea is the same.
- Use
Invokewhen you need synchronous execution on the target thread. - Keep delegated code small and watch for closure-related surprises.

