Cross-thread operation

Oct 12, 2010 at 10:45 AM

I am displaying a 'Tiles loading , Please wait ......'  message by setting the visible property of a label  on both the      OnTileLoadComplete  and OnTileLoadStart    events.

I am now getting a     'Cross-thread operation not valid: Control   accessed from a thread other that the thread it was created on.'

This code worked on an older version of the Gmap, but since upgrading to the latest i am getting this error.

What is the solution please ?

 

Thanks.

Coordinator
Oct 12, 2010 at 12:29 PM

these events are from other thread, use dispatcher/invoker to use it in gui thread

Oct 12, 2010 at 1:08 PM

hmm, never needed to use those before. Googling INVOKE throws up lots of confusion... any chance you can simply paste the

code i need in my UI to handle this event correctly ??  its simply setting the VISIBLE property of a label on my form.

Thanks.

 

Coordinator
Oct 12, 2010 at 1:20 PM

guess why i've invented demos, take a look ;}

Oct 12, 2010 at 2:46 PM

radioman,

im having difficulty in converting this line of code to VB

         MethodInvoker m = delegate()

within the MainMap_OnTileLoadStart  event.

Can you please help ?

 

 

Coordinator
Oct 12, 2010 at 3:34 PM
Private Sub MainMap_OnTileLoadComplete(ByVal ElapsedMilliseconds As Long)
    Me.MainMap.ElapsedMilliseconds = ElapsedMilliseconds
    Dim m As MethodInvoker = Function 
        Me.panelMenu.Text = ("Menu, last load in " & Me.MainMap.ElapsedMilliseconds & "ms")
        Me.textBoxMemory.Text = String.Format(CultureInfo.InvariantCulture, "{0:0.00}MB of {1:0.00}MB", New Object() { Me.MainMap.Manager.MemoryCacheSize, Me.MainMap.Manager.MemoryCacheCapacity })
    End Function
    Try 
        MyBase.BeginInvoke(m)
    Catch obj1 As Object
    End Try
End Sub


Oct 12, 2010 at 3:38 PM

Thanks,, but its this bit that is not compiling,,   im using VS2005 not VS2010  and the

 Dim m As MethodInvoker = Function 
        Me.panelMenu.Text = ("Menu, last load in " & Me.MainMap.ElapsedMilliseconds & "ms")
        Me.textBoxMemory.Text = String.Format(CultureInfo.InvariantCulture, "{0:0.00}MB of {1:0.00}MB", New Object() { Me.MainMap.Manager.MemoryCacheSize, Me.MainMap.Manager.MemoryCacheCapacity })
    End Function

code is not working, i dont know what the equiv is in VS2005.  Online code converters don't convert it correctly,

Thanks,
Coordinator
Oct 12, 2010 at 3:41 PM

whats the error?

Oct 12, 2010 at 3:44 PM

Expression Expected    on the    Dim m as MethodInvoker = Function

causes the rest of the code not to be recognised.

its the syntax thats not supported i think.

Coordinator
Oct 12, 2010 at 3:53 PM

http://stackoverflow.com/questions/2684299/what-is-vb-net-version-of-this-code

Oct 12, 2010 at 4:05 PM

Thanks as always radioman.

 

In case others are using VB.NET / 2005 and come across this problem after upgrading to the latest version, here is the VB.NET code required :

      Private Sub MainMap_OnTileLoadStart() Handles MainMap.OnTileLoadStart
         If InvokeRequired Then
            BeginInvoke(New MethodInvoker(AddressOf ShowPleaseWait))
         Else
            ShowPleaseWait()
         End If
      End Sub

      Private Sub MainMap_OnTileLoadComplete(ByVal elapsedmiliseconds As Long) Handles MainMap.OnTileLoadComplete
         If InvokeRequired Then
            BeginInvoke(New MethodInvoker(AddressOf HidePleaseWait))
         Else
            HidePleaseWait()
         End If
      End Sub

      Public Sub ShowPleaseWait()
         Me.lblLoading.Visible = True
      End Sub

      Public Sub HidePleaseWait()
         Me.lblLoading.Visible = False
      End Sub