witam mam pewien problem ponieważ chciałem stworzyć chat w wpf (ponieważ w konsoli wszystko poszło dobrze chciałem spróbować w wpf) i podczas robienia tego chatu kiedy chciałem dodać kod który będzie odbierał wiadomości od servera pojawiła mi się taka wiadomość "the calling thread cannot access this object because a different thread owns it." i mam do was pytanie co może być przyczyna tego błędu ? Ponizej wklejam kod
#edit za ip specjalnie są x
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Windows.Threading;
namespace Chat_2._0._0
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private NetworkStream ns;
public MainWindow()
{
InitializeComponent();
}
private void Connect_Click(object sender, RoutedEventArgs e)
{
Thread t1 = new Thread(reMessege);
try
{
TcpClient client = new TcpClient();
client.Connect("x.x.x.x", 4040);
ns = client.GetStream();
if (client.Connected)
{
connected.Visibility = Visibility.Visible;
disconnected.Visibility = Visibility.Hidden;
t1.Start();
}
} catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void reMessege()
{
while (true)
{
BinaryFormatter bf = new BinaryFormatter();
string rMessege = (string)bf.Deserialize(ns);
textbox_main.Text += rMessege + "\r\n";
}
}
}
}