Re: Executing a method in a given thread context
- From: "Francois PIETTE" <fpiette@xxxxxxxxxxxxxxxxx>
- Date: Wed, 9 Aug 2006 08:55:27 +0200
Jeffrey,
Thanks for your very interesting answer. I will have a look at the "timer
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 application, 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 trick" to
a worker thread ?
--
Francois PIETTE
http://www.overbyte.be
""Jeffrey Tan[MSFT]"" <jetan@xxxxxxxxxxxxxxxxxxxx> a écrit dans le message
de news:iScdys3uGHA.492@xxxxxxxxxxxxxxxxxxxxxxxx
Hi Francois,thread
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
methods from other threads is using Control.Invoke/BeginInvoke methods, IWinform
know you do not want to use them, but these 2 methods are what .Net
provided for this purpose.to
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
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 });encapsulate
}
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
all the Win32 API declaration in a class for good maintain ability.http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
ications.rights.
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
.
- Follow-Ups:
- Re: Executing a method in a given thread context
- From: David Levine
- Re: Executing a method in a given thread context
- From: "Jeffrey Tan[MSFT]"
- Re: Executing a method in a given thread context
- From: "Jeffrey Tan[MSFT]"
- 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]"
- Executing a method in a given thread context
- Prev by Date: Re: Executing a method in a given thread context
- Next by Date: Re: Reflection, how to tell the complexity of the type???
- 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
|