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

    3 answers 3

    In the BackgroundWorker example, updates occur frequently - at each iteration of the loop. In addition, there is code in the backgroundWorker1_ProgressChanged update handler:

     void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { textBox1.AppendText(e.UserState.ToString()); } 

    Most likely, the constant call to AppendText causes the hang. In theory, in backgroundWorker1_ProgressChanged you need to display the status in percent, and display the result only in backgroundWorker1_RunWorkerCompleted

    PS If in ProgressChanged still need to refer to data that is processed in another thread, specify that it is thread safe.

    BackgroundWorker example in msdn BackgroundWorker example in msdn

    • All the same because of the frequent call. I put System.Threading.Thread.Sleep (5); and it works when I remove the delay, the form hangs - or_die

    You can try to force a UI update via Application.DoEvents , but a better way is to use BackgroundWorker designed specifically for this purpose.

      Most likely the matter is that updates are called too often and the form only does what is updated. Try to do two things - make the timer shorter (500 ms) and limit the execution time of OnTimer to some reasonable time, such as 100-200 ms. In a separate variable, save the point from which to continue the update at the next iteration of the timer. Then between timers the form will have time to be processed and user actions.

      Another option is to insert the update into the Application.Idle handler, but in this case it should be limited in time even more.

      • I tried to play with the interval, all the time hanging ( - or_die