using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Matrix2
{
public partial class Form1 : Form
{
List<System.Threading.Thread> threadList = new List<System.Threading.Thread>();
private System.Threading.Thread _createThread;
private System.Threading.Thread _fallingThread;
private bool createBool = true;
public Form1()
{
InitializeComponent();
threadList.Add(_createThread);
threadList.Add(_fallingThread);
_createThread = new System.Threading.Thread(createThread_Loop);
_fallingThread = new System.Threading.Thread(fallingThread_Loop);
this.Closing += Form1_Closing;
this.ControlAdded += Form1_ControlAdded;
}
private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
//Console.WriteLine("added" + e.Control.Name);
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
foreach (var thread in threadList)
{
try
{
thread.Abort();
}
catch (Exception)
{
}
}
System.Diagnostics.Process.GetCurrentProcess().Kill();
e.Cancel = false;
}
private void Form1_Load(object sender, EventArgs e)
{
_createThread.Start();
_fallingThread.Start();
}
async Task<List<char>> getCharList()
{
List<char> _result = new List<char>();
foreach (char c in File.ReadAllText(Application.StartupPath + "\\charList.txt").ToCharArray())
{
_result.Add(c);
}
return _result;
}
async Task<int> threadLoopSpleep()
{
int result = 0;
result = (3 * (this.Controls.Count / 4)) / 2;
return result;
}
async Task<int> getNewRandom(int start, int end)
{
int result = new Random().Next(start, end);
return result;
}
async void createThread_Loop()
{
var _cList = await getCharList();
int count = 0;
Random _rand1 = new Random(25);
Random _rand2 = new Random(10);
while (createBool)
{
System.Threading.Thread.Sleep(await threadLoopSpleep());
Label label = new Label();
string _text = _cList[new Random().Next(_cList.Count - 1)].ToString();
label.Name = "label_N°" + this.Controls.Count;
label.ForeColor = Color.White;
label.Text = _text.First().ToString();
label.AutoSize = true;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Font = new Font("Microsoft Sans Serif", (_rand1.Next(5,30)),
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.Location = new Point(_rand2.Next(this.Width), 0);
label.Tag = "_main";
Invoke((MethodInvoker)delegate ()
{
this.Controls.Add(label);
if (count > 10)
{
//debug = false;
}
else
{
count += 1;
}
});
}
}
async void fallingThread_Loop()
{
var _cList = await getCharList();
int _step = 20;
while (true)
{
System.Threading.Thread.Sleep(30);
string _consoleResult = "ControlsCount=" + this.Controls.Count;
Console.WriteLine(_consoleResult);
foreach (Control control in this.Controls)
{
if (control.Tag.ToString() == "_main")
{
if (control.Location.Y < this.Height)
{
Label _shadowLabel = await createLabel(control as Label);
Timer _disapearT = new Timer();
_disapearT.Interval = 10;
_disapearT.Tick += delegate (object sender, EventArgs args)
{
if (_shadowLabel.ForeColor.G < _step)
{
Invoke((MethodInvoker)delegate ()
{
_shadowLabel.ForeColor = Color.FromArgb(_shadowLabel.ForeColor.R, 0, _shadowLabel.ForeColor.B);
_disapearT.Stop();
this.Controls.Remove(_shadowLabel);
});
}
else
{
Invoke((MethodInvoker)delegate ()
{
_shadowLabel.ForeColor = Color.FromArgb(_shadowLabel.ForeColor.R, _shadowLabel.ForeColor.G - _step, _shadowLabel.ForeColor.B);
});
}
};
Invoke((MethodInvoker)delegate ()
{
this.Controls.Add(_shadowLabel);
_disapearT.Start();
control.Location = new Point(control.Location.X,control.Location.Y + control.Height);
(control as Label).Text =
_cList[new Random().Next(_cList.Count - 1)].ToString().First().ToString();
});
}
else
{
Invoke((MethodInvoker)delegate ()
{
this.Controls.Remove(control);
});
}
}
}
}
}
async Task<Label> createLabel(Label baseLabel)
{
Label label = new Label();
int _step = 1;
string _text = baseLabel.Text;
Random _rand3 = new Random(15);
label.Name = "label_N°" + this.Controls.Count;
label.ForeColor = Color.FromArgb(0,_rand3.Next(75,195),0);
label.Text = _text.First().ToString();
label.AutoSize = true;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Font = baseLabel.Font;
label.Location = baseLabel.Location;
label.Tag = "_shadow";
return label;
}
}
}