Ahoj mam problem s odpojenim pri ukonecni programu, s odpojenim Tcpklient a networkstream
zkousel jsem to timto zpusobem coz by slo ale nastal problem ze jak odpojim klient.close nebo networkstream.close() tak to vyhodi vyjimku, nevim cim to je, tu vyjimku vyhazuje viz. kod popsane, mozna ze je to tim ze jakmile to odpojim tak se to druhe vlakno snazi cist data pri odpojenem klientovi... nevim jak to vyresit prosim poradte a omlouvam se za malo citelny kod
protected override void OnClosed(EventArgs e)
{
nas.Close();
klient.Close();
base.OnClosed(e);
}
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 System.Net.Sockets;
using System.Windows.Media.Animation;
using System.Net;
using System.IO;
using System.Threading;
using ClassLibrary3;
namespace VIAPISKVORKY
{
public partial class MainWindow : Window
{
private int x = 0;
private int y = 0;
private TcpClient klient;
private Thread proc;
private NetworkStream ns;
public delegate void Delegat(int n, int m);
private Delegat deleg;
public static int a = 10;
public static int b = 10;
private int[,] pole = new int[a, b];
private Checkclass kontrola = new Checkclass();
private Mybutton[,] polebtn = new Mybutton[a, b];
private Mybutton[] polebtn1 = new Mybutton[5];
private bool stav=true;
private Mybutton buttonn;
public MainWindow()
{
InitializeComponent();
textBlock1.Text = "Pripoj";
vytvorplochu();
plochaviteze();
proc = new Thread(new ThreadStart(slys));
deleg = new Delegat(dej);
}
public void vytvorplochu()
{
for (int i = 0; i < 10; i++)
{
for (int c = 0; c < 10; c++)
{
Mybutton button = new Mybutton();
button.Margin = new Thickness(x, y, 0, 0);
button.X = c;
button.Y = i;
polebtn[c, i] = button;
button.Click += new RoutedEventHandler(button_Click);
button.Width = 20;
button.Height = 20;
canvas1.Children.Add(button);
x += 20;
}
x = 0;
y += 20;
}
}
public void plochaviteze()
{
int kam = 0;
for (int d = 0; d < 5; d++)
{
buttonn = new Mybutton();
buttonn.Margin = new Thickness(kam, 204, 0, 0);
buttonn.Width = 20;
buttonn.Height = 20;
buttonn.Background = Brushes.DarkBlue;
buttonn.X = d;
polebtn1[d] = buttonn;
canvas1.Children.Add(buttonn);
kam += 40;
}
}
public void animace(Mybutton aRectangle,int x,int y,int cas)
{
NameScope.SetNameScope(this, new NameScope());
TranslateTransform animatedTranslateTransform =
new TranslateTransform();
aRectangle.RenderTransform = animatedTranslateTransform;
this.RegisterName(
"AnimatedTranslateTransform", animatedTranslateTransform);
DoubleAnimationUsingKeyFrames translationAnimation
= new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames translationAnimation1
= new DoubleAnimationUsingKeyFrames();
translationAnimation.BeginTime = new TimeSpan(0, 0, cas);
translationAnimation1.BeginTime = new TimeSpan(0, 0, cas);
translationAnimation.KeyFrames.Add(
new LinearDoubleKeyFrame(
x,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))
);
translationAnimation1.KeyFrames.Add(
new LinearDoubleKeyFrame(
y,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))
);
Storyboard.SetTargetName(translationAnimation, "AnimatedTranslateTransform");
Storyboard.SetTargetName(translationAnimation1, "AnimatedTranslateTransform");
Storyboard.SetTargetProperty(
translationAnimation, new PropertyPath(TranslateTransform.XProperty));
Storyboard.SetTargetProperty(
translationAnimation1, new PropertyPath(TranslateTransform.YProperty));
// Create a storyboard to apply the animation.
Storyboard translationStoryboard = new Storyboard();
translationStoryboard.Children.Add(translationAnimation);
translationStoryboard.Children.Add(translationAnimation1);
translationStoryboard.Begin(aRectangle);
}
public void dej(int n, int m)
{
polebtn[n, m].Background = Brushes.Red;
textBlock1.Text = "Hraj";
}
public void button_Click(object sender, RoutedEventArgs e)
{
Mybutton send = (Mybutton)sender;
if (stav == true && pole[send.X, send.Y] != 1 && pole[send.X, send.Y] != 2)
{
stav = false;
textBlock1.Text = "Nehraj";
send.Background = Brushes.Black;
pole[send.X, send.Y] = 1;
Stack<int> zas = new Stack<int>();
if (kontrola.kontroluj(1, pole,zas) == 1)
{
textBlock1.Text = "Vyhral";
int plus = 0;
int cas = 1;
for (int k = 0; k < 5; k++)
{
int y=zas.Pop();
int x=zas.Pop();
animace(polebtn1[k], ((x) * 20) - plus, ((y) * 20) - 204,cas);
cas += 1;
plus = plus+40;
}
}
string xx = send.X.ToString();
string yy = send.Y.ToString();
pripoj(xx, yy);
}
}
public void pripoj(string xx, string yy)
{
try
{
ns = klient.GetStream();
Byte[] x;
x = System.Text.Encoding.ASCII.GetBytes(xx + "," + yy);
ns.Write(x, 0, x.Length);
}
catch (InvalidOperationException error)//tady haze vyjimku
{
MessageBox.Show(error.Message);
if (ns != null)
ns.Close();
}
catch (Exception error1)
{
MessageBox.Show(error1.Message);
if (ns != null)
ns.Close();
}
//ns.Close();
}
public void slys()
{
if (klient != null)
{
while (true)
{
try
{
string[] words;
ns = klient.GetStream();
byte[] data = new byte[1024];
ns.Read(data, 0, data.Length);
string xxx = System.Text.Encoding.ASCII.GetString(data);
words = xxx.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
stav = true;
int fir = Int32.Parse(words[0]);
int two = Int32.Parse(words[1]);
pole[fir, two] = 2;
this.Dispatcher.Invoke(deleg, new object[] { fir, two });
}
catch (InvalidOperationException error)
{
MessageBox.Show(error.Message);
if(ns!=null)
ns.Close();
}
catch(Exception error1)
{
MessageBox.Show(error1.Message);
if (ns != null)
ns.Close();
}
}
}
}
private void button_Click1(object sender, EventArgs e)
{
try
{
klient = new TcpClient(ip.Text, Int32.Parse(port.Text));
{
proc.Start();
textBlock1.Text = "Jed";
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("/VIAPISKVORKY;component/Images/zamek2.jpg", UriKind.Relative);
bi3.EndInit();
image1.Stretch = Stretch.Fill;
image1.Source = bi3;
}
}
catch (SocketException err)
{
MessageBox.Show(err.Message);
if (klient !=null)
klient.Close();
}
catch (ArgumentException err1)
{
MessageBox.Show(err1.Message);
}
catch (Exception err2)
{
MessageBox.Show(err2.Message);
if(klient !=null)
klient.Close();
}
}
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
button1.Background = Brushes.Blue;
}
}
}