Re: Custom keywords and compiler-supplied code
- From: "Jon" <jon.shadforth@xxxxxxxxxxxxxx>
- Date: 21 Dec 2005 05:20:46 -0800
Hi - thanks for the responses.
Just a few final thoughts...
1. Having to sprinkle GUI code with InvokeRequired tests means the GUI
looses focus. Also, a third-party control may not have thread-awareness
built into its event handlers. Therefore if you wired a threaded event
to it you'd have a problem. Making the threaded code have optional
synchronisation on its event-firing is one good way to alleviate this.
2. By not having the ability to have customised compiler-generated code
all of the fully generic solutions require the use of
ISynchronizeInvoke.Invoke or Delegate.DynamicInvoke, etc. These are all
late-bound calls and are significantly slower than calling a delegate
directly. I.e. the C# compiler and JIT compiler between them have
features that cannot be exploited by an end-programmer. I 100% agree
that keyword bloat is bad - I'm not asking for that. What I want is
some way to emulate the compiler's ability to inject code and have
intellisense pick it up.
3. Because C# generics are supported by the run-time (whereas C++
templates are processed at compile-time), it is not possible to exploit
them fully to solve this problem. I.e. a generic method cannot get
access to a compiler-supplied Invoke method on a delegate, only the
DynamicInvoke method which of course is late-bound and slow.
4. Regarding the runtime handling of a delegate's Invoke method, I was
referring to the following IL example:
..method public hidebysig newslot virtual
instance int32 Invoke(int32 a,
int32 b) runtime managed
{
} // end of method MyDelegate::Invoke
[This is generated from "public delegate int MyDelegate(int a, int
b);"]
I was wondering what the JIT compiler does with this - out of
curiousity :)
As a very final comment, here is a sample of code (from my threading
class) I'm now using in my test application. This allows the event to
be used directly if a synchronous call can be made, otherwise an
asynchronous call is made.
Note: evOnTick is an event, eventSync is of type ISynchronizeInvoke.
if (evOnTick != null)
{
if ((eventSync == null) ||
(eventSync.InvokeRequired == false))
{
// fast and direct call to the handler[s]
evOnTick(this, eventArgs);
}
else
{
// asychronous call, slow but safe!
eventSync.BeginInvoke(evOnTick, new object[] {
this, eventArgs });
}
}
Thanks again for feedback,
Jon.
.
- References:
- Prev by Date: Re: datatable.HasChanges() ?
- Next by Date: Re: C#.NET Obfuscator
- Previous by thread: Re: Custom keywords and compiler-supplied code
- Next by thread: Re: Custom keywords and compiler-supplied code
- Index(es):
Relevant Pages
|