C# 中的自定义事件
我从我的上一个问题中取得了一些进展: Event Text Postback from Multithreaded Class to Windows ActiveForm
我也取得了一些进展这个: http://www.codeproject.com/KB/cs/simplesteventexample.aspx
但我再次陷入困境,代码似乎没有触发事件或正确处理它(我真的很新自定义事件)。
这是我目前拥有的代码。
public class TextArgs : EventArgs
{
private string CurrentText;
public string Text
{
set
{
CurrentText = value;
}
get
{
return this.CurrentText;
}
}
}
class Scan
{
public event TextHandler Text;
public delegate void TextHandler(Scan s, TextArgs e);
private void ProcessDirectory(String targetDirectory, DateTime cs)
{
SetScanHistory(targetDirectory);
// Does a bunch of stuff...
}
// EDIT: Forgot this bit of code; thanks for pointing this out :)
// Sets the text of scan history in the ui
private void SetScanHistory(string text)
{
if (Text != null)
{
TextArgs TH = new TextArgs();
TH.Text = text;
Text(this, TH);
}
}
// Does more stuff...
}
我的 Windows 窗体:
public partial class MyWinForm: Form
{
private void NewScan(Object param)
{
Scan doScan = new Scan();
doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked));
doScan.Text += new Scan.TextHandler(SetText);
}
// Sets the text of txtScanHistory to the text
private void SetText(Scan s, TextArgs e)
{
// Invoke is always required (which is intended)
this.Invoke((MethodInvoker)delegate
{
txtScanHistory.Text += e.Text + Environment.NewLine;
});
}
}
同样,我没有看到任何错误,但文本框根本没有更新。我确信我没有写任何东西,我只是对自定义事件主题非常无知,我不知道如何解决这个问题。
I have made some progress from my last question here: Event Text Postback from Multithreaded Class to Windows ActiveForm
I also made some progress with this: http://www.codeproject.com/KB/cs/simplesteventexample.aspx
But I am stuck again, the code doesn't seem to be triggering the event or properly handling it (I am really new to custom events).
Here is what I currently have code wise.
public class TextArgs : EventArgs
{
private string CurrentText;
public string Text
{
set
{
CurrentText = value;
}
get
{
return this.CurrentText;
}
}
}
class Scan
{
public event TextHandler Text;
public delegate void TextHandler(Scan s, TextArgs e);
private void ProcessDirectory(String targetDirectory, DateTime cs)
{
SetScanHistory(targetDirectory);
// Does a bunch of stuff...
}
// EDIT: Forgot this bit of code; thanks for pointing this out :)
// Sets the text of scan history in the ui
private void SetScanHistory(string text)
{
if (Text != null)
{
TextArgs TH = new TextArgs();
TH.Text = text;
Text(this, TH);
}
}
// Does more stuff...
}
My Windows Form:
public partial class MyWinForm: Form
{
private void NewScan(Object param)
{
Scan doScan = new Scan();
doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked));
doScan.Text += new Scan.TextHandler(SetText);
}
// Sets the text of txtScanHistory to the text
private void SetText(Scan s, TextArgs e)
{
// Invoke is always required (which is intended)
this.Invoke((MethodInvoker)delegate
{
txtScanHistory.Text += e.Text + Environment.NewLine;
});
}
}
So again, I am not seeing any errors, but the textbox is not updating at all. I am sure I am not doing something write, I am just ignorant enough on the topic of custom events I am not sure how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在运行代码之前订阅该事件:
You need to subscribe to the event before running the code:
除了一个微小但非常重要的细节之外,您的代码是正确的。您将在执行您希望监视的代码后订阅事件处理程序。将您的方法
NewScan
更改为在注册事件处理程序后调用StarScan
:Your code is correct, except for one tiny, yet very important, detail. You are subscribing to the event handler AFTER the code you expect to monitor is executed. Change your method
NewScan
to callStarScan
after the event handler is registered: