Mam taki kod serwera:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using Microsoft.Maps.MapControl.WPF;
using BingMapsWinForm;
namespace testserver
{
public partial class Form1 : Form
{
public static Socket s;
public static Socket cli;
public Thread srv;
public Thread klient;
public int counter = 0;
delegate void SetTextCallback(string text);
public Form1()
{
InitializeComponent();
mapUserControl1.MyMap.CredentialsProvider = new ApplicationIdCredentialsProvider("ydugsyugkhi34y34ugui43g42hidy7siuh");
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);
s.Bind(new IPEndPoint(IPAddress.Parse(tb_IP.Text), Convert.ToInt32(tb_port.Text)));
s.Listen(5);
if (s.IsBound)
{
srv = new Thread(new ThreadStart(this.start_server));
srv.Start();
}
else
{
SetText(".");
}
}
void start_server()
{
while (true)
{
Socket cli = s.Accept();
counter++;
this.SetText("Polaczono z klientem nr " + counter);
mapUserControl1.AddPushpin(new Location(0,0),"k","kk");
klient = new Thread(new ParameterizedThreadStart(this.new_client));
klient.Start(cli);
}
}
void new_client(object o)
{
Socket cli = (Socket)o;
mapUserControl1.AddPushpin(new Location(0, 0), "k", "kk");
while (cli.IsBound)
{
try
{
byte[] buffer = new byte[1024];
int wynik = cli.Receive(buffer);
if (wynik == 0)
{
cli.Close();
break;
}
String time = Encoding.ASCII.GetString(buffer, 0, wynik);
this.SetText("Client >> " + time);
String respond = time;
byte[] responddata = Encoding.ASCII.GetBytes(respond.ToString());
wynik = cli.Send(responddata);
if (wynik == 0)
{
cli.Close();
break;
}
}
catch { }
}
}
private void SetText(string text)
{
if (this.rtb_comm.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.rtb_comm.Text += text + (char)0x0D;
}
}
//private void AddPin(
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
}
}
a to kod kontrolki mapy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl.WPF;
namespace BingMapsWinForm
{
/// <summary>
/// Interaction logic for MapUserControl.xaml
/// </summary>
public partial class MapUserControl : UserControl
{
public MapUserControl()
{
InitializeComponent();
}
private void PanMap_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
Point p;
MyMap.TryLocationToViewportPoint(MyMap.Center, out p);
if (p != null)
{
switch (b.Tag as string)
{
case "Up":
p.Y -= 50;
break;
case "Down":
p.Y += 50;
break;
case "Left":
p.X -= 50;
break;
case "Right":
p.X += 50;
break;
}
Microsoft.Maps.MapControl.WPF.Location l;
MyMap.TryViewportPointToLocation(p, out l);
MyMap.SetView(l, MyMap.ZoomLevel);
}
}
public void AddPushpin(Location latlong, string title, string description)
{
Pushpin p = new Pushpin()
{
Location = latlong,
Tag = new Metadata()
{
Title = title,
Description = description
}
};
p.MouseDown += PinClicked;
DataLayer.Children.Add(p);
}
private void PinClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Pushpin p = sender as Pushpin;
Metadata m = (Metadata)p.Tag;
//Ensure there is content to be displayed before modifying the infobox control
if (!String.IsNullOrEmpty(m.Title) || !String.IsNullOrEmpty(m.Description))
{
InfoboxTitle.Text = m.Title;
InfoboxDescription.Text = m.Description;
Infobox.Visibility = Visibility.Visible;
MapLayer.SetPosition(Infobox, p.Location);
}
}
private void CloseInfobox_Click(object sender, RoutedEventArgs e)
{
Infobox.Visibility = System.Windows.Visibility.Collapsed;
}
/// <summary>
/// Simple class for storing pushpin information
/// </summary>
public struct Metadata
{
public string Title;
public string Description;
}
}
}
w metodzie start_server chce dodać do kontrolki nowego pushpina kiedy podłączy się nowy klient. Nie mogę tego jednak zrobić bo wyskakuje błąd w momencie podłączenia klienta: "Wątkiem wywołującym musi być STA, ponieważ wiele składników interfejsów użytkownika go wymaga." Co robić? Pomóżcie dobrzy ludzie:P