Witam.
Mam pytanie, jak sprawić, aby nowy wątek, który będzie dodawał na przykład tekst do kontrolki w okienku - nie blokował tego okienka? Żeby można było je przesuwać itd?
Próbowałem to zrobić tak:
using System;
using System.Threading;
using System.Windows.Forms;
namespace csharp_watki
{
public partial class Form1 : Form
{
public delegate void addText();
public addText addTextDelegate;
public Thread myThread;
public Form1()
{
InitializeComponent();
addTextDelegate = new addText(addListItem);
}
public void addListItem()
{
String myItem;
for (int i = 0; i < 6; i++)
{
myItem = "Linia numer: " + i.ToString();
log.Items.Add(myItem);
log.Update();
Thread.Sleep(1000);
}
}
private void threadFunction()
{
myThreadClass myThreadClassObject = new myThreadClass(this);
myThreadClassObject.Run();
}
private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(threadFunction));
myThread.Start();
}
}
public class myThreadClass
{
Form1 formcontrol1;
public myThreadClass(Form1 forma)
{
formcontrol1 = forma;
}
public void Run()
{
formcontrol1.Invoke(formcontrol1.addTextDelegate);
}
}
}
Ale to nie gra ;/ Tekst się dodaje, ale nie można w tym czasie ruszać okienkiem. Jak to w takim razie zrealizować?