M
M
markula2015-02-24 12:34:56
WPF
markula, 2015-02-24 12:34:56

Why does the application hang and why does serialization not work?

I am writing a coursework in WPF. Bouncing text app. When you click on the form, the text should stop/start jumping. You also need to implement serialization.
There were 2 problems:
1. When you click on the forms (to pause / resume the flow in which the text jumps), the application may hang (sometimes hangs, sometimes not).
2. When trying to serialize the application by clicking on the button, an exception occurs:

Необработанное исключение типа "System.InvalidOperationException" в System.Xml.dll Дополнительные сведения: Возникла ошибка при отражении типа "MyNervousText.MainWindow".

Source:
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.Threading;
using System.Xml.Serialization;
using System.IO;
namespace MyNervousText
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    [Serializable]
    public partial class MainWindow : Window
    {
        public static Label lbl1 = new Label();
        public static Label timer = new Label();
        public static Thread killme = null;
        public static Thread timerenew;
        public static string s;
        public static ImageBrush ib = new ImageBrush();
        public static BitmapImage bi = new BitmapImage();
        public static Boolean threadSuspended = false;
        public static Button serialize = new Button();
        public string data;
        //метод вызывающий главное окно
        public MainWindow()
        {
            InitializeComponent();
            bi.BeginInit();
            bi.UriSource = new Uri("c:/users/markula/documents/visual studio 2013/Projects/MyNervousText/MyNervousText/Resources/Синий экран.png");
            bi.EndInit();
            this.Content = appLogic.grd;
            appLogic.setGrid();
            this.Width = 1000;
            this.Height = 1000;
            this.Background = ib;
            ib.ImageSource = bi;
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;


            serialize.Height = 50;
            serialize.Width = 100;
            serialize.Content = "сохранить";
            serialize.Click += serialize_Click;

            //Поток часов
            timerenew = new Thread(Runt);
            timerenew.Start();
            //Поток текста
            killme = new Thread(Run);
            killme.Start();
            //События
            this.Closing += MainWindow_Closing; // Подписываемся на событие "закрытие окна"
            this.MouseDown += MainWindow_MouseDown; // Подписываемся на событие "нажатие мыши"
        }
        //метод запусающий поток Runt
        public void Runt()
        {
            this.Dispatcher.BeginInvoke((ThreadStart)delegate()
            {                
                appLogic.setTime();
            });
        }
        //метод запусающий поток Killme
        public void Run()
        {
            while (killme != null)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(50));//задержка 50мс
                paint();
            }
        }
        //Метод отрисовки текста
        public void paint()
        {
            this.Dispatcher.BeginInvoke((ThreadStart)delegate()
            {
                appLogic.paint();

            });
        }

        //нажатие кнопки serialize
        public void serialize_Click(object sender, RoutedEventArgs e)
        {
            SaveAsXmlFormat(this, @"MnWndSave");
        }
        //метод сериализирующий приложение
        public static void SaveAsXmlFormat(object objGraph, string fileName)
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(MainWindow));
            using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                xmlFormat.Serialize(fStream, objGraph);
            }
        }

        //метод закрытия окна
        private void MainWindow_Closing(object sender,System.ComponentModel.CancelEventArgs e)
        {
            appLogic.close();
        }

        //метод нажатия кнопки мыши, который должен приостанавливать/возобновлять поток
        private void MainWindow_MouseDown(object sender, MouseEventArgs e)
        {
            appLogic.ssprsm();
        }
   }
    public class appLogic
    {
        public static Grid grd = new Grid();
        public static void setGrid()
        {
            //сетка:создание столбцов и строк
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();
            grd.ColumnDefinitions.Add(colDef1);
            grd.ColumnDefinitions.Add(colDef2);
            grd.ColumnDefinitions.Add(colDef3);
            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();
            grd.RowDefinitions.Add(rowDef1);
            grd.RowDefinitions.Add(rowDef2);
            grd.RowDefinitions.Add(rowDef3);
            //сетка:добавление метки lbl1
            Grid.SetRow(MainWindow.lbl1, 0);
            Grid.SetColumn(MainWindow.lbl1, 0);
            grd.Children.Add(MainWindow.lbl1);
            //сетка:добавление метки timer
            Grid.SetRow(MainWindow.timer, 0);
            Grid.SetColumn(MainWindow.timer, 2);
            grd.Children.Add(MainWindow.timer);
            //сетка:добавление кнопки serialize
            Grid.SetRow(MainWindow.serialize, 3);
            Grid.SetRow(MainWindow.serialize, 3);
            grd.Children.Add(MainWindow.serialize);
        }
        public static void setTime()
        {
                MainWindow.timer.Content = DateTime.Now;
                MainWindow.timer.Foreground = Brushes.Blue;
                MainWindow.timer.FontSize = 40;
                MainWindow.timer.HorizontalAlignment = HorizontalAlignment.Right;
        }
        public static void paint()
        {
            MainWindow.lbl1.Foreground = Brushes.White;
            MainWindow.lbl1.Content = "Прыгающий Текст";
            MainWindow.lbl1.FontSize = 30;
            MainWindow.s = MainWindow.lbl1.Content.ToString();
            for (int i = 0; i < MainWindow.s.Length; i++)
            {
                Random rnd = new Random();
                double rand = rnd.NextDouble();
                MainWindow.lbl1.Margin = new Thickness(rand * 1 + 1 * i, rand * 5 + 13, 0, 0);
            }
        }
        public static void close()
        {
            if (MainWindow.threadSuspended)
            {
                MainWindow.killme.Resume();
            }
            MainWindow.killme.Abort();
            MainWindow.timerenew.Abort();
        }
        public static void ssprsm()
        {
            if (MainWindow.threadSuspended)
            {
                MainWindow.killme.Resume();
            }
            else
            {
                MainWindow.killme.Suspend();
            }
            MainWindow.threadSuspended = !MainWindow.threadSuspended;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sintez, 2015-02-24
@sintez

1. Why use threads in this task, especially two? Look in the direction of DispatcherTimer , and transfer all the clock drawing logic to the Tick event handler. Bouncing text would be better implemented with the animation tools that WPF provides. Read about Storyboard .
2. I don't understand why serialize the form itself. Is this really what the job requires? Perhaps you wanted to serialize the state of the application and not the form itself?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question