By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Cross-thread operation not valid

P: n/a
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
Jul 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jul 31 '06 #2

P: n/a
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:
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
Jul 31 '06 #3

P: n/a
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/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.

Aug 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.