Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / August 2006

Tip: Looking for answers? Try searching our database.

Cross-thread operation not valid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jack - 29 Jul 2006 23:04 GMT
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
Jeffrey Tan[MSFT] - 31 Jul 2006 09:24 GMT
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.
Brian Gideon - 31 Jul 2006 15:32 GMT
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

> 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
[quoted text clipped - 8 lines]
>
> Jack
Jeffrey Tan[MSFT] - 01 Aug 2006 02:51 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.