C# 中的自定义事件

发布于 2024-12-12 07:16:57 字数 1943 浏览 0 评论 0原文

我从我的上一个问题中取得了一些进展: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2024-12-19 07:16:57

您需要在运行代码之前订阅该事件:

private void NewScan(Object param)
{
    Scan doScan = new Scan();
    // Change the order so you subscribe first!
    doScan.Text += new Scan.TextHandler(SetText);
    doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked));
}

You need to subscribe to the event before running the code:

private void NewScan(Object param)
{
    Scan doScan = new Scan();
    // Change the order so you subscribe first!
    doScan.Text += new Scan.TextHandler(SetText);
    doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked));
}
蓝眸 2024-12-19 07:16:57

除了一个微小但非常重要的细节之外,您的代码是正确的。您将在执行您希望监视的代码后订阅事件处理程序。将您的方法 NewScan 更改为在注册事件处理程序后调用 StarScan

private void NewScan(Object param) 
{ 
    Scan doScan = new Scan(); 
    doScan.Text += new Scan.TextHandler(SetText); 
    doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked)); 
}

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 call StarScan after the event handler is registered:

private void NewScan(Object param) 
{ 
    Scan doScan = new Scan(); 
    doScan.Text += new Scan.TextHandler(SetText); 
    doScan.StarScan(Convert.ToInt32(checkBoxBulk.Checked)); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文