Зависает программа с отрисковкой линий (простые часы)
Написал простейшие часы, нужно как то сделать автоматическую отрисовку. Если всё повесить на кнопку, вроде работает. Засунул в while, после нескольких секунд программа зависает. Что можно придумать?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Clock
{
public partial class Form1 : Form
{
public void Animation()
{
System.Threading.Thread.Sleep(100);
Pen red = new Pen(Color.Red, 2);
Pen black = new Pen(Color.Black, 2);
Pen blue = new Pen(Color.Blue, 2);
Graphics gPanel = panel1.CreateGraphics();
int length = 70;
DateTime date1;
date1 = DateTime.Now;
gPanel.Clear(Color.White);
gPanel.DrawLine(red, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Second * 6 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Second * 6 * Math.PI / 180 - 90 * Math.PI / 180))));
gPanel.DrawLine(blue, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Minute * 6 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Minute * 6 * Math.PI / 180 - 90 * Math.PI / 180))));
gPanel.DrawLine(black, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Hour * 30 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Hour * 30 * Math.PI / 180 - 90 * Math.PI / 180))));
string time = date1.ToString().Split(' ')[1];
string date = date1.ToString().Split(' ')[0];
label1.Text = $"{time}\n{date}";
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
while (true) Animation();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Ответы (1 шт):
Автор решения: Cr1stal
→ Ссылка
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Clock
{
public partial class Form1 : Form
{
public void Animation()
{
Pen red = new Pen(Color.Red, 2);
Pen black = new Pen(Color.Black, 2);
Pen blue = new Pen(Color.Blue, 2);
Graphics gPanel = panel1.CreateGraphics();
int length = 70;
DateTime date1;
date1 = DateTime.Now;
gPanel.Clear(Color.White);
gPanel.DrawLine(red, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Second * 6 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Second * 6 * Math.PI / 180 - 90 * Math.PI / 180))));
gPanel.DrawLine(blue, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Minute * 6 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Minute * 6 * Math.PI / 180 - 90 * Math.PI / 180))));
gPanel.DrawLine(black, new Point(panel1.Height / 2, panel1.Width / 2), new Point((int)(panel1.Height / 2 + length * Math.Cos(date1.Hour * 30 * Math.PI / 180 - 90 * Math.PI / 180)), (int)(panel1.Width / 2 + length * Math.Sin(date1.Hour * 30 * Math.PI / 180 - 90 * Math.PI / 180))));
string time = date1.ToString().Split(' ')[1];
string date = date1.ToString().Split(' ')[0];
label1.Text = $"{time}\n{date}";
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
async private void panel1_Paint(object sender, PaintEventArgs e)
{
while (true) { await Task.Delay(100); Animation(); }
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}