Windows Develop Bookmark and Share   
 index > Windows Forms General > Cross-thread operation not valid with splashscreen
 

Cross-thread operation not valid with splashscreen

Hi,


I have a splashscreen which i am loading on application start.
This is loading good when it does not have anything on it.
Now, i have to add a mappoint on the splash screen.(ssmap)
When i add the map point to splash screen, i get the following error:

Cross-thread operation not valid: Control 'ssmap' accessed from a thread other than the thread it was created on.

does anyone have an idea why i am getting this error?

Thanks in advance

rowter  Wednesday, September 09, 2009 12:30 AM
Right.

That would seem to imply the COM control doesn't like being accessed from the thread the splash screen is on. Problem is, I'm not sure what you can do. You appear to be creating the control on the same thread as the splash screen, so that shouldn't be a problem, and if you try to create the control on a different thread (say the one that started the app)you won't be able to add it to the splash screen since child controls must be on the same thread as the parent. I don't see how using Invoke as suggested would help at this point.

Is the main thread of your applicationmarked as STA ? If so, then I'm afraid I'm out of ideas exept to suggest you can't do what you're trying to do, but that seems like a pretty dumb limitation of VB .Net Splash Screen feature if that's the case.

Hopefully someoneelse knows more than I do.

....

Just found a suggestion on the web (may not be reliable) that sometimes VB .Net's Splash Screens are shown/activated on a different thread than they were created on. Seems unlikely, but you could try wrapping the code in Form_Load in an invoke call, something like;

Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

me.Invoke(new System.Windows.Forms.MethodInvoker(loadmappoint())

End Sub

Note... I usually work in C#, so my syntax might be off a little. Not sure if this will help anyway, but it's worth a shot.
Yort  Wednesday, September 09, 2009 9:01 PM
Nope.
Post code some of what you are doing so that someone can take a look.
Help someone to help you.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Wednesday, September 09, 2009 12:34 AM


Project->right click properties-> Under application -> select the splash screen

This is the code behind the splash screen.

Public NotInheritable Class ssmap

Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Call loadmappoint()

End Sub

Public Sub loadmappoint()


AMC.NewMap(MapPoint.GeoMapRegion.geoMapNorthAmerica)
Dim objmap = AMC.ActiveMap
Me.Activate()

End Sub

End Class

rowter  Wednesday, September 09, 2009 1:13 AM
Hi,

This is just a guess, but that looks like a COM component and COM has various restrictions on it's threading model etc, so it's possible you can't use a COM component on a splash screen where the splash screen form is created on a thread other than the main one used to start the application. However, that is just a guess, I have no experience with VB's splash screens.

I assume you have the method that is the entry to your application marked with the STA attribute ?

There doesn't appear to be anything wrong with your code, as far as I can tell, which is why I suspect some sort of COM related issue.

Does the same code work on a form that isn't a splash screen ?
Yort  Wednesday, September 09, 2009 1:25 AM
Hi,

I do not get the error if i place mappoint on the form.

Thanks
rowter  Wednesday, September 09, 2009 3:29 AM
Hi,

Just to clarify... you don't get the error if you place it on a non-splash screen form, or if you place it on a form rather than creating it yourself in the load event ?
Yort  Wednesday, September 09, 2009 3:33 AM
you get this error if you try to access a control on windows forms from different thread..

create a function which performs the reqired operation and create an event handler to that function..and
when you want to modify the control instead of directly accessing the control invoke the event by using

this.Invoke(<EventHandler>)


i think this may help you..
Bharath kumar Y.S  Wednesday, September 09, 2009 8:56 AM
Hi Yort,

I placed it on a non splash screen form adn in a button click event. This way, i did not get the error.

Thanks
rowter  Wednesday, September 09, 2009 2:35 PM
Right.

That would seem to imply the COM control doesn't like being accessed from the thread the splash screen is on. Problem is, I'm not sure what you can do. You appear to be creating the control on the same thread as the splash screen, so that shouldn't be a problem, and if you try to create the control on a different thread (say the one that started the app)you won't be able to add it to the splash screen since child controls must be on the same thread as the parent. I don't see how using Invoke as suggested would help at this point.

Is the main thread of your applicationmarked as STA ? If so, then I'm afraid I'm out of ideas exept to suggest you can't do what you're trying to do, but that seems like a pretty dumb limitation of VB .Net Splash Screen feature if that's the case.

Hopefully someoneelse knows more than I do.

....

Just found a suggestion on the web (may not be reliable) that sometimes VB .Net's Splash Screens are shown/activated on a different thread than they were created on. Seems unlikely, but you could try wrapping the code in Form_Load in an invoke call, something like;

Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

me.Invoke(new System.Windows.Forms.MethodInvoker(loadmappoint())

End Sub

Note... I usually work in C#, so my syntax might be off a little. Not sure if this will help anyway, but it's worth a shot.
Yort  Wednesday, September 09, 2009 9:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Highlight CheckedListBox Item
• ToolStripComboBox catch {Enter} Keypress
• Printing help needed ?
• Event Publisher not Publishing
• DOM - Minimum or Maximum width restrictions for a child window
• Opening another form only if it isn't open already.
• Finding index from cursor position in a textbox
• redirection of forms in wimdows application
• Why GridView control requests its Cell value by CellValueNeeded event rather than a template method?
• foreach(Form f in AllformsOfApplication)