NK
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Thread thread;
private bool start = true; // Флаг для выхода из цикла
private void listener() //Потоковый метод
{
int count = 0;
while (start)
{
count++;
if (InvokeRequired)
{ //Инвок
Invoke(new Action(() =>
{
Log.Text += count.ToString() + "\r\n"; //Обращаемся к текстовому полю, что в основном потоке
}));
}
};
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
thread = new Thread(listener);
thread.IsBackground = true;
thread.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
start = false;
// if (thread.IsAlive) // Если поток жив, ликвидируем его.
// {
// thread.Abort();
// }
}
}
}