C#:字符串作为事件的参数?
我的表单有一个GUI线程和另一个计算线程 表单有一个richtTextBox。我希望工作线程将字符串传递给表单,以便每个字符串都显示在文本框中 每次在工作线程中生成一个新字符串时,我调用一个事件,现在应该显示该字符串。 但我不知道怎么传弦!这就是我迄今为止所尝试的:C#:字符串作为事件的参数?,c#,.net,multithreading,events,event-handling,C#,.net,Multithreading,Events,Event Handling,我的表单有一个GUI线程和另一个计算线程 表单有一个richtTextBox。我希望工作线程将字符串传递给表单,以便每个字符串都显示在文本框中 每次在工作线程中生成一个新字符串时,我调用一个事件,现在应该显示该字符串。 但我不知道怎么传弦!这就是我迄今为止所尝试的: ///// Form1 private void btn_myClass_Click(object sender, EventArgs e) { myClass myObj = new myClass(); myOb
///// 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;
});
}
您需要通过继承非EventArgs来创建一个新类。一般来说,您需要继承
EventArgs
并添加string
属性,然后使您的事件类型为EventHandler
,但这是BackgroundWorker
的典型情况
此处示例:
在这里:
创建自己版本的
EventArgs
这样做:
public class MyEventArgs : EventArgs
{
public string MyEventString {get; set; }
public MyEventArgs(string myString)
{
this.MyEventString = myString;
}
}
///// myClass (working thread...)
class myClass
{
public event EventHandler NewListEntry;
public void ThreadMethod()
{
DoSomething();
}
protected virtual void OnNewListEntry(MyEventArgs 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";
OnNewListEntry(new MyEventArgs(test));
}
}
然后在代码中,将EventArgs
替换为MyEventArgs
,并使用字符串创建一个MyEventArgs
对象
然后,您可以使用MyEventArgs
实例.MyEventString
访问它
所以你可以这样做:
public class MyEventArgs : EventArgs
{
public string MyEventString {get; set; }
public MyEventArgs(string myString)
{
this.MyEventString = myString;
}
}
///// myClass (working thread...)
class myClass
{
public event EventHandler NewListEntry;
public void ThreadMethod()
{
DoSomething();
}
protected virtual void OnNewListEntry(MyEventArgs 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";
OnNewListEntry(new MyEventArgs(test));
}
}
以您的形式:
private void myObj_NewListEntry(Object objSender, MyEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
// Here I want to add my string from the worker-thread to the textbox!
richTextBox1.Text += e.MyEventString;
});
}
像这样
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));
}
}
}
在订阅表格中
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
类设置为不可变的,因为这是有意义的。我还部分更正了您的命名约定,在方便的情况下进行了简化和更正。子类EventArgs
,并将您的字符串添加为属性。谢谢,但我在这里遇到错误“与'declaration'匹配的最佳重载方法有一些无效参数”。BeginInvoke(NewListEntryEventHandler,sender,e);我的错误是,应该使用委托而不是方法组。我改进了我的代码。当我添加crawler.NewListEntry+=crawler\u NewListEntry时,OMG事件是一个陷阱;对于我的Form1,我得到错误:没有与委托系统相匹配的重载。EventHandler'??