Hi Jack,
Thanks for your post!
Since you are using Elapsed event, I am assuming that you are using
System.Timers.Timer class. This timer will fire the Elapsed event in
another thread instead of the main thread. While your .Net Winform controls
are all STA, which are not thread-safe, they can not be invoked by another
thread, so this error message is generated. This is by design.
In .Net winform area, the correct way to deal with cross-thread calling is
marshal the method calling with Control.Invoke method. Sample code like
this:
Public Delegate Sub FormCloseEventHandler()
Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
If Form1.InvokeRequired Then
Form1.Invoke(New FormCloseEventHandler(AddressOf Form1.Close))
Else
Form1.Close()
End If
End Sub
Alternately, you may set Timer1.SynchronizingObject property to the
control/form. This is the buildin synchronization method provided by
System.Timers.Timer class.
Finally, please refer to the article below for more information regarding
difference between various timer class in .Net:
"Comparing the Timer Classes in the .NET Framework Class Library"
http://msdn.microsoft.com/msdnmag/is...2/TimersinNET/
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.