Is there any documentation on exactly how expensive it is:
if (x.IsInvokeRequired)
{
beginInvoke . . . .
}
Any metrics around this would be great.
Use a profiler
I think you need to consider the relative costs of both functions vs. the absolute cost of InvokeRequired.
InvokeRequired for Control essentially compares the current ThreadId with the expected ThreadId. If you look in reflector the code is slightly more complex but in effect that's what it's doing. This is fairly cheap as it's just a few function calls and a comparison.
BeginInvoke involves taking several locks, adding delegates to an invoke queue and potentially a Marshal between threads. This code is much more expensive relative to the actual InvokeRequired call (likely an order of magnitude or 2). You would need a great deal more calls where InvokeRequired returns true before you would see any gain by just going straight to BeignInvoke.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With