C # WinForm. Thread starts and runs, but blocks all form elements. What am I doing wrong? If there is a need, I can show the code, it is short.
namespace InvokeTest { public partial class MyForm:Form { public delegate void AddListItem(); public AddListItem myDelegate; private Thread myThread; int count; public bool bStop; public MyForm() { InitializeComponent(); myDelegate += new AddListItem(AddListItemMethod); count = 0; bStop = true; } public void AddListItemMethod() { String myItem; while(!bStop) { ++count; myItem = "MyListItem" + count.ToString(); myListBox.Items.Add(myItem); myListBox.Update(); Thread.Sleep(1000); } myThread.Abort(); } private void button1_Click(object sender, EventArgs e) { myThread = new Thread(new ThreadStart(ThreadFunction)); bStop = false; myThread.Start(); } private void ThreadFunction() { myListBox.Invoke(myDelegate);//моя вставка } private void button2_Click(object sender, EventArgs e) { textBox1.Text = "Stop"; bStop = true; } } }
AddListItemMethodwithSleep's in the UI thread. - VladD