Witam serdecznie.
Piszę aplikacje, która pobiera sobie z telefonu współrzędne GPS a następnie co 3 sekundy wyświetla pina na mapie.
Wszystko fajnie działa, dopóki nie podłącze drugiego klienta. Dostaję wtedy błąd:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
Jeśli nie wywołuję wątku "map", a tym samym nie umieszczam pinów na mapie, to wszystko działa.
Przyznam szczerze, że dopiero odkrywam wątki więc proszę o wyrozumiałość i pomoc.
Oto kod:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private TcpListener tcpListener;
private Thread listenThread;
private int pinCounter = -1;
private Pushpin[] pinList = new Pushpin[10];
string c;
string[] words;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ResizeMode = ResizeMode.NoResize;
this.tcpListener = new TcpListener(IPAddress.Any, 7);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void map()
{
this.Dispatcher.BeginInvoke((ThreadStart)delegate()
{
for (int i = 0; i < 10; i++)
{
if (pinList[i] != null)
{
if (Map.Children.Count != 0)
{
var pushpin = Map.Children.OfType<Pushpin>().FirstOrDefault(p => ("locationPushpin"+pinCounter).Equals(p.Tag));
if (pushpin != null)
{
Map.Children.Remove(pushpin);
}
}
Map.Children.Add(pinList[i]);
}
}
});
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[200];
int bytesRead;
pinCounter++;
while (true)
{
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 200);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
tcpClient.Close();
break;
}
c = Encoding.ASCII.GetString(message, 0, bytesRead);
words = c.Split('\n');
this.Dispatcher.BeginInvoke((ThreadStart)delegate()
{
Pushpin pin = new Pushpin();
pinList[pinCounter] = pin;
pinList[pinCounter].Tag = "locationPushpin" + pinCounter.ToString();
pinList[pinCounter].Location = new Location(Double.Parse(words[1], CultureInfo.InvariantCulture), Double.Parse(words[2], CultureInfo.InvariantCulture));
textBox1.Text = "Coords: " + words[1] + ", " + words[2];
ToolTipService.SetToolTip(pinList[pinCounter], words[0] + "\n" + words[1] + "\n" + words[2] + "\n" + pinCounter.ToString());
Thread mapThread = new Thread(new ThreadStart(map));
mapThread.Start();
}
);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Map.ZoomLevel += 1;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Map.ZoomLevel -= 1;
}
}
}