Исправить ошибку в коде (сериализация/десериализация) - C#
Формулировка задачи:
Имеется код. Компилируется, но при попытке открыть файл/сохранить выдает ошибку:
Необработанное исключение типа "System.Threading.ThreadStateException" в System.Windows.Forms.dll
Вот, собственно, сам код:
Простите за дубль, не знал в какой именно раздел обращаться.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace Point
{
class Point
{
protected int x;
protected int y;
public Point()
{
x = 5;
y = 5;
}
public Point(int k, int g)
{
x = k;
y = g;
}
public Point(Point p)
{
x = p.x;
y = p.y;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void setx(int t)
{
x = t;
}
public void sety(int t)
{
y = t;
}
public double distance(Point p)
{
return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
public void plus(Point p)
{
x = x + p.x;
y = y + p.y;
}
public bool testconv(Point p)
{
if ((x == p.x) && (y == p.y)) return true;
else return false;
}
public void displayin()
{
Console.WriteLine(" enter point\n");
x = y = Convert.ToInt32(Console.ReadLine());
}
public virtual void displayout()
{
Console.WriteLine(string.Format("Point({0},{1})\n", x, y));
}
class PColor : Point
{
private string c;
public override void displayout()
{
Console.WriteLine("Color " + c);
}
public PColor()
{
c = "white";
}
public PColor(int x1, int y1, string c1) : base(x1, y1)
{
c = c1;
}
public PColor(PColor p) : base(p)
{
c = p.c;
}
}
/*static void Main(string[] args)
{
int x, y;
Point a = new Point();
a.displayout();
a.displayin();
Console.WriteLine("you entered: " + a.getx() + " " + a.gety());
a.displayout();
Console.WriteLine("copy constructor test: ");
Point b = new Point(a);
b.displayout();
Console.WriteLine("enter x,y:\n");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
a.setx(x);
a.sety(y);
Console.WriteLine("setter test: ");
a.displayout();
Console.WriteLine("parameter constructor test: ");
Point c = new Point(x, y);
c.displayout();
Console.WriteLine("distance: " + c.distance(b));
if (b.testconv(c)) Console.WriteLine("they are equal\n");
else Console.WriteLine("they are not equal\n");
Console.WriteLine("adding points result: ");
a.plus(b);
a.displayout();
PColor f = new PColor();
f.displayout();
PColor f1 = new PColor(3, 3, "green");
f1.displayout();
PColor f2 = new PColor(f1);
f2.displayout();
Console.ReadKey();
}*/
}
class Program : Form
{
private List<Point> points;
public Program()
{
points = new List<Point>();
Menu = new MainMenu();
MenuItem menuFile = new MenuItem("File");
Menu.MenuItems.Add(menuFile);
MenuItem menuOpen = new MenuItem("Open");
menuOpen.Click += new EventHandler(this.onOpen);
menuFile.MenuItems.Add(menuOpen);
MenuItem menuSaveAs = new MenuItem("Save as");
menuSaveAs.Click += new EventHandler(this.onSaveAs);
menuFile.MenuItems.Add(menuSaveAs);
this.Paint += new PaintEventHandler(this.onPaint);
this.MouseClick += new MouseEventHandler(this.onClick);
}
private void onOpen(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.DefaultExt = "pnt";
fd.Filter = "Text files (*.pnt)|*.pnt|All files (*.*)|*.*";
fd.ShowDialog();
if (fd.FileName != "")
{
points.Clear();
Stream s = File.OpenRead(fd.FileName);
BinaryReader r = new BinaryReader(s);
int size = r.ReadInt32();
for (int i = 0; i < size; i++)
{
int x = r.ReadInt32();
int y = r.ReadInt32();
points.Add(new Point(x, y));
}
r.Close();
Refresh();
}
}
private void onSaveAs(object sender, EventArgs e)
{
SaveFileDialog fd = new SaveFileDialog();
fd.DefaultExt = "graph";
fd.Filter = "Text files (*.pnt)|*.pnt|All files (*.*)|*.*";
fd.ShowDialog();
if (fd.FileName != "")
{
Stream s = File.OpenWrite(fd.FileName);
BinaryWriter w = new BinaryWriter(s);
w.Write((Int32)points.Count);
points.ForEach(delegate (Point point) {
w.Write((Int32)point.getx());
w.Write((Int32)point.gety());
});
w.Close();
}
}
private void onPaint(object sender, PaintEventArgs e)
{
points.ForEach(delegate (Point point) {
e.Graphics.FillRectangle(Brushes.Black, point.getx(), point.gety(), 1, 1);
});
}
private void onClick(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
points.Add(point);
Refresh();
}
public static void Main(string[] args)
{
Application.Run(new Program());
}
}
}Решение задачи: «Исправить ошибку в коде (сериализация/десериализация)»
textual
Листинг программы
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Program());
}