GMapOverlay in class exception

Topics: Windows Forms
Aug 7, 2011 at 1:47 PM

hi!

(I'm using the example to continue my project.)

I'm creating a tracking system and I have a class to create and control each user. I'm sending the GMapOverlay as reference to the constructor of the class, but when I try to add a marker in class it throws an exception.

Can anyone help me please! 

tks ;)

Aug 7, 2011 at 2:18 PM

lets use telepathy and find out ;} ..damn can't see any exceptions, try to help yourself by pasting some useful info

Aug 7, 2011 at 2:31 PM

lol sorry... :P

********In main form I have********

 

GMapOverlay objects;

....

TerminalClient user = new TerminalClient(TcpListener.AcceptTcpClient(), objects);

**************In the class I have*****************

    class TerminalClient    {       

    public static Hashtable AllClients = new Hashtable();

    private TcpClient _client;

    private string _clientIP;

    private string _clientID;

    GMapMarker _marker;

    GMapOverlay _mapObjects;
    private string partialStr;
    private byte[] data;
    public TerminalClient(TcpClient client, GMapOverlay mapObjects)        { 
           _mapObjects = mapObjects;
            _client = client;
           _clientIP = client.Client.RemoteEndPoint.ToString();
            AllClients.Add(_clientIP, this);
           data = new byte[_client.ReceiveBufferSize];            client.GetStream().BeginRead(data, 0,            System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);        

}

and the this method in TerminalClass:

 public void ReceiveMessage(IAsyncResult ar)        {

.....

_marker = new GMapMarkerGoogleGreen(new PointLatLng(37.12869663086, -8.7073064722));                               

_mapObjects.Markers.Add(_marker);

this is where it throws the exception.

Aug 7, 2011 at 2:39 PM

nice progress ;] now what's the exception?

Aug 7, 2011 at 2:46 PM

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dllStep into: Stepping over non-user code 'System.Exception.ToString'System.InvalidOperationException: A operação entre threads não é válida: controlo 'textBoxMarkersCounter' acedido a partir de um thread diferente do thread onde foi criado.   em System.Windows.Forms.Control.get_Handle()   em System.Windows.Forms.Control.set_WindowText(String value)   em System.Windows.Forms.TextBoxBase.set_WindowText(String value)   em System.Windows.Forms.Control.set_Text(String value)   em System.Windows.Forms.TextBoxBase.set_Text(String value)   em System.Windows.Forms.TextBox.set_Text(String value)   em IST.Tracking.MainForm.Markers_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) em C:\Users\Nelson\Documents\Visual Studio 2008\Projects\IST.Tracking\IST.Tracking\Forms\MainForm.cs:line 447   em GMap.NET.ObjectModel.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)   em GMap.NET.ObjectModel.ObservableCollectionThreadSafe`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) em C:\Users\Nelson\Documents\Visual Studio 2008\Projects\IST.Tracking\GMap.NET.WindowsForms\GMap.NET.ObjectModel\ObservableCollectionThreadSafe.cs:line 43   em GMap.NET.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index) em C:\Users\Nelson\Documents\Visual Studio 2008\Projects\IST.Tracking\GMap.NET.WindowsForms\GMap.NET.ObjectModel\ObservableCollection.cs:line 463   em GMap.NET.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) em C:\Users\Nelson\Documents\Visual Studio 2008\Projects\IST.Tracking\GMap.NET.WindowsForms\GMap.NET.ObjectModel\ObservableCollection.cs:line 432   em System.Collections.ObjectModel.Collection`1.Add(T item)   em IST.Tracking.TerminalClient.ReceiveMessage(IAsyncResult ar) em C:\Users\Nelson\Documents\Visual Studio 2008\Projects\IST.Tracking\IST.Tracking\Source\TerminalClient.cs:line 110Step into: Stepping over non-user code 'System.Exception.ToString'

 

Is it because the class is created in a separated thread? what should I do? 

Aug 7, 2011 at 4:30 PM

do not update markers in non-user thread, use backgroundWarker or invoke