Re: Executing a method in a given thread context
- From: "Francois PIETTE" <fpiette@xxxxxxxxxxxxxxxxx>
- Date: Mon, 14 Aug 2006 09:34:17 +0200
You might also look at the base class
System.Threading.SynchronizationContext.
I looked at MSDN for the description. It is not very clear to me how I use
it.
Do you have any sample code, preferably using it from Socket.BeginReceive
callback ? As you knoiw, when the callback is run, it is in the context of a
worker thread created by the system. From this callback, I want to raise an
event in the context of the thread which called Socket.BeginReceive at the
first place (not necessary the mainthread).
--
Francois PIETTE
http://www.overbyte.be
"David Levine" <SnipHereDlevinenntp2AndHere@xxxxxxxxx> a écrit dans le
message de news:eHEFzgzvGHA.4384@xxxxxxxxxxxxxxxxxxxxxxx
You might also look at the base classThe
System.Threading.SynchronizationContext. It provides some convenient
abstractions for sending or posting messages between arbitrary threads.
nice part is that it does not care if the receiving thread has a window orfrom
not, it is the responsibility of the implementation of classes derived
SynchronizationContext to forward the message to the receiving thread.Each
implementation of a SynchronizationContext must provide its own means ofderived
delivering the message within the correct synchronization context by
providing its own implementation of the methods Send and Post. The default
Post uses a threadpool thread for delivering the callback; for Send it
invokes it directly (no synchronization).
The class System.Windows.Forms.WindowsFormsSynchronizationContext is
from this base class, and is used to synchronize messages sent to thethe
application (it uses control.Invoke/BeginInvoke). The classes
System.Windows.Forms.Application and System.Windows.Forms.Control both use
this; internally it uses a Send or PostMessage to forward the message to
windowa
I used SynchronizationContext to provide a way for clients to subscribe to
generic event so that the client did not have to worry about synchronizing"timer
within the callback - when the client subscribes to the event it captures
the caller's sync context and then uses that when it fires the event.
It seems to work well; it is only available in 2.0.
Dave
"Francois PIETTE" <fpiette@xxxxxxxxxxxxxxxxx> wrote in message
news:%23FsLCH4uGHA.1772@xxxxxxxxxxxxxxxxxxxxxxx
Jeffrey,
Thanks for your very interesting answer. I will have a look at the
application,trick".
This raise some more questions:
1) What is the impact of relying on a winform control when my non-visual
component is used in a non GUI application such as a console
trick"a
service application, a web application, or whatever ?
2) What is the impact of using P/Invoke ? I there anything to avoid ?
Should
I use some attributes to mark the assemblies or the classes as safe and
secure ?
3) Is the "UI thread" the main thread of an application or - as said by
someone in a previous answer to my question - any thread which has a
message
pump ? As I understand, it is the user interface thread aka the main
thread.
4) If answer to 3 is "main thread", then how do I apply the "timer
messageto
a worker thread ?
--
Francois PIETTE
http://www.overbyte.be
""Jeffrey Tan[MSFT]"" <jetan@xxxxxxxxxxxxxxxxxxxx> a écrit dans le
communityde news:iScdys3uGHA.492@xxxxxxxxxxxxxxxxxxxxxxxx
Hi Francois,
Before I read this thread, you have several discussion with the
Win32members alrealdy. To not miss the key point, I want to recap my
understand
of this issue.
Based on my understanding, you want to know some best practise of
cross-thread operation in .Net winform and you do not like the pure
Ip/invoke approach.thread
Yes, just as you have known, the only recommended way of invoking UI
methods from other threads is using Control.Invoke/BeginInvoke methods,
classknow you do not want to use them, but these 2 methods are what .NetWinform
provided for this purpose.
If you do not want the component user to take care of the cross-thread
Control.Invoke/BeginInvoke requirement, you'd better refer to
System.Timers.Timer class implementation model. System.Timers.Timer
setnormally runs Elapsed event in another non-UI thread, however, it
provided
a SynchronizingObject to deal with this. Once SynchronizingObject is
toto
certain Control reference, this Elapsed event will be executed in that
control's UI thread without Timer class user aware of. This is very
convinient for the class/component user and I think is what you wanted
toachieve. To achieve this, the Timer class uses MyTimerCallback method
object[]call the Elapsed event like this:
private void MyTimerCallback(object state)
{
....
try
{
if (this.onIntervalElapsed != null)
{
if ((this.SynchronizingObject != null) &&
this.SynchronizingObject.InvokeRequired)
{
this.SynchronizingObject.BeginInvoke(this.onIntervalElapsed, new
you{
this, args1 });
}
else
{
this.onIntervalElapsed(this, args1);
}
}
}
catch (Exception)
{
}
...
}
Then in the Elapsed event, the user can write code does not care of the
UI
thread operation issue. You may use this design as well.
Additionally, there is rule regarding not use p/invoke. Actually, as
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifhave found out, .Net winform internally p/invokes Win32 API to completeencapsulate
must task. So if you want, you may use p/invoke freely. You may
all the Win32 API declaration in a class for good maintain ability.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
theications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
situationsmost efficient resolution. The offering is not appropriate for
bestthat require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
contactinghandled working with a dedicated Microsoft Support Engineer by
Microsoft Customer Support Services (CSS) atrights.
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
.
- Follow-Ups:
- Re: Executing a method in a given thread context
- From: David Levine
- Re: Executing a method in a given thread context
- References:
- Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: AJ
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: AJ
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: "Jeffrey Tan[MSFT]"
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- Re: Executing a method in a given thread context
- From: David Levine
- Executing a method in a given thread context
- Prev by Date: Re: Executing a method in a given thread context
- Next by Date: Re: Catching managed exceptions in unmanaged code
- Previous by thread: Re: Executing a method in a given thread context
- Next by thread: Re: Executing a method in a given thread context
- Index(es):
Relevant Pages
|
|