|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cross-thread operation not validI have an application (COM Add-in) that in the initialization code it
displays a splash screen. I want the splash screen to close after n seconds , so I add a timer to the splash screen and attempt to close it from the Elapsed event. And I get the Cross-thread error. Conceptually I think I know why this error occurs, I am just not sure how to solve it, since the form was shown from a section of code that is no longer running. Any ideas would be most appreciated. Jack 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/issues/04/02/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/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. Jack,
Setting the SynchronizingObject property of System.Timers.Timer is the best option IMO. Though, you could use System.Windows.Forms.Timer as well. Brian Jack wrote: Show quoteHide quote > I have an application (COM Add-in) that in the initialization code it > displays a splash screen. I want the splash screen to close after n seconds > , so I add a timer to the splash screen and attempt to close it from the > Elapsed event. And I get the Cross-thread error. > > Conceptually I think I know why this error occurs, I am just not sure how to > solve it, since the form was shown from a section of code that is no longer > running. > > Any ideas would be most appreciated. > > Jack Yes, I agree. For System.Timers.Timer, SynchronizingObject property is the
best choice. However, for System.Threading.Timer, it does not have a SynchronizingObject property, so marshaling the call with Control.Invoke is the only choice :-). 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.
Check result of call into Windows API
Listview columnheader question Print ID card using Plastic Card Printer in VB.net Multi-Threaded App Date and Time Calculation. Regex doesn't match when test string is in middle of file Winforms User COntrol Addhandler in a Multithreading class and the events raised in seperate threads, how to get to the ca How Do I Create an Entry Point for a DLL? Saving an image from the browser |
|||||||||||||||||||||||