Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading; // Обязательно подключаем для работы с потоками
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- private Thread MyCustomText; // глобальная переменная нашего потока, для доступа из других участков кода к нему
- public Form1()
- {
- InitializeComponent();
- MyCustomText = new Thread(Zagolovok); //создаём новый поток из функции 'Zagolovok', и присваеваем его значение нашему потоку.
- MyCustomText.Start(); // запускаем его командой Start();
- }
- void Zagolovok()
- {
- string MyText = "Строкаа "; // бегущая строка
- while (true) // бесконечный цикл
- {
- Thread.Sleep(1000); // цикл ждёт каждую секунду своего оборота
- Invoke(new MethodInvoker(delegate // используем делегат для доступа к textbox1 из другого потока. Иначе выдаст ошибку
- {
- if (textBox1.Text.Length == MyText.Length)
- {
- textBox1.Text = null;
- }
- else
- {
- textBox1.Text += MyText.ToCharArray()[textBox1.Text.Length].ToString();
- }
- }));
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- MyCustomText.Abort(); // останавливаем поток при закрытии формы (чтобы процесс завершился)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement