I can not deal with threads and form controls. When I create a separate stream, I calculate calculations in it, and update the controls in the main thread, the form hangs until the control is updated. The same thing, when I call from secondary threads through delegates to the controls, the form all the time hangs. For example, here.
private void button6_Click(object sender, EventArgs e) { Thread t = new Thread(test); t.Start(); } private void test() { this.akk = File.ReadAllLines(this.fileName); while(i < this.akk.Length) { l.Add(this.akk[i]); i++; } } private void timer1_Tick(object sender, EventArgs e) { for(int i = 0; i< this.l.Count; i++) textBox3.AppendText(l[i]); }
I tried through BackgroundWorker, it turned out
Test t = new Test(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { textBox1.AppendText(e.UserState.ToString()); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 1000; i++) backgroundWorker1.ReportProgress(i, t.var = i); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("Completed"); } } public class Test { public int var { get; set; } }
And vserovno form hangs until the loop is completed and will not write everything in textBox