C #: ¿Cadena como parámetro para el evento?
Tengo un hilo GUI para mi formulario y otro hilo que calcula las cosas.
El formulario tiene un richtTextBox. Quiero que el subproceso de trabajo pase cadenas al formulario para que todas las cadenas se muestren en el cuadro de texto.
Cada vez que se genera una nueva cadena en el subproceso de trabajo que llamo un evento, y esto ahora debería mostrar la cadena. Pero no sé cómo pasar la cadena! Esto es lo que intenté hasta ahora:
///// Form1
private void btn_myClass_Click(object sender, EventArgs e)
{
myClass myObj = new myClass();
myObj.NewListEntry += myObj_NewListEntry;
Thread thrmyClass = new Thread(new ThreadStart(myObj.ThreadMethod));
thrmyClass.Start();
}
private void myObj_NewListEntry(Object objSender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
// Here I want to add my string from the worker-thread to the textbox!
richTextBox1.Text += "TEXT"; // I want: richTextBox1.Text += myStringFromWorkerThread;
});
}
///// myClass (working thread...)
class myClass
{
public event EventHandler NewListEntry;
public void ThreadMethod()
{
DoSomething();
}
protected virtual void OnNewListEntry(EventArgs e)
{
EventHandler newListEntry = NewListEntry;
if (newListEntry != null)
{
newListEntry(this, e);
}
}
private void DoSomething()
{
///// Do some things and generate strings, such as "test"...
string test = "test";
// Here I want to pass the "test"-string! But how to do that??
OnNewListEntry(EventArgs.Empty); // I want: OnNewListEntry(test);
}
}