System.InvalidOperationException: "Object is currently in use elsewhere."
using System;
using System.Drawing;
namespace ЖИЗА
{
public partial class Form1 : Form
{
public static int[,] cellmap = new int[200,200];
public static int test = 1;
static Graphics g;
static Random rnd = new Random();
public Form1()
{
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
cellmap[i, j] = 0;
}
}
InitializeComponent();
}
public static void aboba(object obj)
{
int x1 = rnd.Next(0, 199);
int y1 = rnd.Next(0, 199);
cellmap[x1, y1] = 1;
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
if (cellmap[i, j] != 0)
{
**g.DrawRectangle(new Pen(Color.Black, 3), i*2, j * 2, 2, 2);**
}
}
}
}
private void Form1_Paint_1(object sender, PaintEventArgs e)
{
int num = 0;
g = CreateGraphics();
// создаем таймер
TimerCallback tcb = new TimerCallback(aboba);
System.Threading.Timer timer = new System.Threading.Timer(tcb, num, 0, 100);
}
}
}
Изучаю drawing в c# winforms
Какое-то время все нормально работает, код рисует квадраты в случайных местах, но если расширить экран или просто в случайный момент времени строка g.DrawRectangle(new Pen(Color.Black, 3), i*2, j * 2, 2, 2); выдается эта ошибка, что делать?
Я пока не знаю что с этим делать