ここでの最後の質問からいくつかの進歩がありました:マルチスレッドクラスからWindowsActiveFormへのイベントテキストのポストバック
私もこれでいくらかの進歩を遂げました: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;
});
}
}
繰り返しになりますが、エラーは表示されませんが、テキストボックスはまったく更新されていません。私は何かを書いているのではないと確信しています。カスタムイベントのトピックについては十分に無知です。これを修正する方法がわかりません。