我在这里的最后一个问题取得了一些进展: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;
});
}
}
再说一次,我没有看到任何错误,但文本框根本没有更新。我确定我没有写任何东西,我只是对自定义事件的话题一无所知,我不知道如何解决这个问题。