J
Jon
I'm investiging multi-threaded GUI applications; specifically the
technique used to ensure that controls are only modified under the
correct thread. The standard technique is to use
if(InvokeRequired == true) ... BeginInvoke(...) ...
I have some code that ensures that an event fired from a worker
(non-GUI) thread will use the appropriate BeginInvoke call for
synchronisation. By offloading the synchronisation onto the worker
thread the GUI event handlers can be written safe in the knowledge that
they will -only- be called from the correct thread. This leaves the GUI
code a lot more focussed and less cluttered, etc.
However, what I'd really like to do is create my own custom C# keyword,
say thread_safe_delegate, (ignore non-standard naming conventions -
this is for illusation only). I'd like the compiler to process this in
much the same way as it does with the delegate keyword. (I.e. emit code
into the assembly to produce a private and embedded class; ideally the
intellisense system should also be able to pick up on this).
The end result would be that I could declare a GUI-safe event something
like this..
thread_safe_delegate void MyDelegate(int i);
event MyDelegate myEvent;
The technique I use in my code (mentioned above) for synchronisation is
to provide to my worker thread with a synchronisation GUI object, for
example:
Control eventSync;
Given the thread-safe delegate, an event, and a synchronisation object,
I would then be able to say...
void Test(int x)
{
myEvent(eventSync, x);
}
The compiler-generated delegate class would take care of invoking the
event handler(s) either directly (when the caller is on the GUI thread)
or asynchronously (using BeginInvoke when on a worker thread).
So.. the questions are:
1. Is it possible to define a new keyword and the code it should emit?
2. Could intellisense be made aware of this?
3. Is the C# compiler's support for the delegate keyword hard-coded or
part of a more customisable framework?
4. Does anyone know how the runtime resolves methods of
compiler-supplied delegate classes (specifically the Invoke and
BeginInvoke methods which appear 'stubbed-out' in the generated IL)?
Any feedback appreciated,
Jon.
technique used to ensure that controls are only modified under the
correct thread. The standard technique is to use
if(InvokeRequired == true) ... BeginInvoke(...) ...
I have some code that ensures that an event fired from a worker
(non-GUI) thread will use the appropriate BeginInvoke call for
synchronisation. By offloading the synchronisation onto the worker
thread the GUI event handlers can be written safe in the knowledge that
they will -only- be called from the correct thread. This leaves the GUI
code a lot more focussed and less cluttered, etc.
However, what I'd really like to do is create my own custom C# keyword,
say thread_safe_delegate, (ignore non-standard naming conventions -
this is for illusation only). I'd like the compiler to process this in
much the same way as it does with the delegate keyword. (I.e. emit code
into the assembly to produce a private and embedded class; ideally the
intellisense system should also be able to pick up on this).
The end result would be that I could declare a GUI-safe event something
like this..
thread_safe_delegate void MyDelegate(int i);
event MyDelegate myEvent;
The technique I use in my code (mentioned above) for synchronisation is
to provide to my worker thread with a synchronisation GUI object, for
example:
Control eventSync;
Given the thread-safe delegate, an event, and a synchronisation object,
I would then be able to say...
void Test(int x)
{
myEvent(eventSync, x);
}
The compiler-generated delegate class would take care of invoking the
event handler(s) either directly (when the caller is on the GUI thread)
or asynchronously (using BeginInvoke when on a worker thread).
So.. the questions are:
1. Is it possible to define a new keyword and the code it should emit?
2. Could intellisense be made aware of this?
3. Is the C# compiler's support for the delegate keyword hard-coded or
part of a more customisable framework?
4. Does anyone know how the runtime resolves methods of
compiler-supplied delegate classes (specifically the Invoke and
BeginInvoke methods which appear 'stubbed-out' in the generated IL)?
Any feedback appreciated,
Jon.