Re: Executing a method in a given thread context
- From: jetan@xxxxxxxxxxxxxxxxxxxx ("Jeffrey Tan[MSFT]")
- Date: Wed, 09 Aug 2006 06:15:13 GMT
Hi Francois,
Before I read this thread, you have several discussion with the community
members 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 Win32
p/invoke approach.
Yes, just as you have known, the only recommended way of invoking UI thread
methods from other threads is using Control.Invoke/BeginInvoke methods, I
know you do not want to use them, but these 2 methods are what .Net Winform
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 class
normally runs Elapsed event in another non-UI thread, however, it provided
a SynchronizingObject to deal with this. Once SynchronizingObject is set to
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 to
achieve. To achieve this, the Timer class uses MyTimerCallback method to
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 object[] {
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 you
have found out, .Net winform internally p/invokes Win32 API to complete
must task. So if you want, you may use p/invoke freely. You may encapsulate
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
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
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 the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- Follow-Ups:
- Re: Executing a method in a given thread context
- From: Francois PIETTE
- 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
- Executing a method in a given thread context
- Prev by Date: Reflection, how to tell the complexity of the type???
- Next by Date: Re: Executing a method in a given thread context
- 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
|
Loading