I am working on a Windows Forms application and contains custom controls with methods that could potentially be called from threads other than a UI thread. Therefore, these methods look a bit to exclude exceptions:
public void DoSomeStuff() { if (InvokeRequired) { Invoke((Action)DoSomeStuff); } else {
The explicit involvement of the DoSomeStuff group of methods in Action attracted my attention, and so I studied delegates and other related topics more deeply than before.
Although I saw some related questions, I could not find the exact answer to my question:
Why in this case, the DoSomeStuff method DoSomeStuff requires an explicit cast to Action ?
If I remove the cast, then I get two errors:
Error 102 Argument 1: Cannot convert from 'method group' to 'System.Delegate'
Error 101 The best overloaded method match for 'System.Windows.Forms.Control.Invoke (System.Delegate, params object [])' has some invalid arguments
The fact that the compiler seems to be confused about which overload of Invoke to use seems like a pretty big hint, but I'm still not sure why this can't figure it out. I expect the compiler to deduce that the first Invoke overload, which takes one Delegate argument, is the one that should be used.
I would expect, because there is no problem if the code is written as follows:
Action a = DoSomeStuff; Invoke(a);
The DoSomeStuff method DoSomeStuff can be implicitly converted to the Action delegate type, and Action outputs (technically?) From System.Delegate , so Invoke can handle the argument a without any problems. But then why can't the implicit conversion be done by the compiler when I try to pass the DoSomeStuff as an argument directly? Honestly, I'm not convinced of my logic here, but I'm still not sure what I am missing.