C#:String作为事件的参数? [英] C#: String as parameter to event?
问题描述
表单中有一个richtTextBox。我希望工作线程将字符串传递给表单,以便每个字符串都显示在文本框中。
每次在工作线程中生成一个新的字符串,我调用一个事件,现在应该显示字符串。
但我不知道如何传递字符串!这是我迄今为止所尝试的:
///// 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
{
/ /这里我想把我的字符串从工作线程添加到文本框!
richTextBox1.Text + =TEXT; //我想要: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()
{
/////做一些事情并生成字符串,如测试。 ..
string test =test;
//这里我想通过test-string!但是怎么办呢?
OnNewListEntry(EventArgs.Empty); //我想要:OnNewListEntry(test);
}
}
这个
public class NewListEntryEventArgs:EventArgs
{
private readonly string test;
public NewListEntryEventArgs(string test)
{
this.test = test;
}
public string Test
{
get {return this.test; }
}
}
然后你就这样声明你的类>
class MyClass
{
public delegate void NewListEntryEventHandler(
object sender,
NewListEntryEventArgs args );
public event NewListEntryEventHandler NewListEntry;
protected virtual void OnNewListEntry(string test)
{
if(NewListEntry!= null)
{
NewListEntry(this,new NewListEntryEventArgs(test) );
}
}
}
在订阅 form
private void btn_myClass_Click(object sender,EventArgs e)
{
MyClass myClass = new MyClass();
myClass.NewListEntry + = NewListEntryEventHandler;
...
}
private void NewListEntryEventHandler(
object sender,
NewListEntryEventArgs e)
{
if(richTextBox1 .invokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
this.NewListEntryEventHandler(sender,e);
});
return;
}
richTextBox1.Text + = e.Test;
}
自由使 NewListEntryEventArgs
类不可变,因为这是有道理的。我也部分地纠正了您的命名约定,简化并在哪些方面进行了更正。
I have a GUI-thread for my form and another thread that computes things.
The form has a richtTextBox. I want the worker-thread to pass strings to the form, so that every string is displayed in the textbox.
Everytime a new string is generated in the worker thread I call an event, and this should now display the string. But I don't know how to pass the string! This is what I tried so far:
///// 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);
}
}
Like this
public class NewListEntryEventArgs : EventArgs
{
private readonly string test;
public NewListEntryEventArgs(string test)
{
this.test = test;
}
public string Test
{
get { return this.test; }
}
}
then you declare your class like this
class MyClass
{
public delegate void NewListEntryEventHandler(
object sender,
NewListEntryEventArgs args);
public event NewListEntryEventHandler NewListEntry;
protected virtual void OnNewListEntry(string test)
{
if (NewListEntry != null)
{
NewListEntry(this, new NewListEntryEventArgs(test));
}
}
}
and in the subscribing Form
private void btn_myClass_Click(object sender, EventArgs e)
{
MyClass myClass = new MyClass();
myClass.NewListEntry += NewListEntryEventHandler;
...
}
private void NewListEntryEventHandler(
object sender,
NewListEntryEventArgs e)
{
if (richTextBox1.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
this.NewListEntryEventHandler(sender, e);
});
return;
}
richTextBox1.Text += e.Test;
}
I've taken the liberty of making the NewListEntryEventArgs
class immutable, since that makes sense. I've also partially corrected your naming conventions, simplified and corrected where expedient.
这篇关于C#:String作为事件的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!