Re: Executing a method in a given thread context



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,

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.



.



Relevant Pages

  • RE: POPUp - VB.Net
    ... how does your timer interact with the threads in your ... Private Sub Button1_Click(ByVal sender As System.Object, ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.vsnet.general)
  • RE: Cross-thread operation not valid
    ... In .Net winform area, the correct way to deal with cross-thread calling is ... difference between various timer class in .Net: ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: .NET C# Winforms question about window opening
    ... Then fire off your web service from the timer ... BackgroundWorker class which makes this task easy. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: One-shot high-resolution POSIX timer periodically late
    ... small bursts due to network jitter (typical average rate of 1000 packets ... Just before I re-send a packet, I arm a one-shot timer in order to ... Linux Plug and Play Support v0.97 Adam Belay ...
    (Linux-Kernel)
  • NForce2: 2.6.0-test11-bk6 UP high interrupt error count with lapic noapic
    ... It is necessary to have both the APIC timer and the 8254 timer running ... Can the 8254 timer be disabled if the APIC timer is running. ... ATAPI CD-ROM, with removable media ... Linux Plug and Play Support v0.97 Adam Belay ...
    (Linux-Kernel)