如果编写了文本,是否有一个TextWriter子类触发事件? [英] Is there a TextWriter child class that fires event if text is written?

查看:80
本文介绍了如果编写了文本,是否有一个TextWriter子类触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个接受TextWriter作为参数的方法(通常是Console.Out,但不一定).

I have written a method that accepts a TextWriter as an argument (Usually Console.Out, but not necessarily).

当我调用此方法时,一些进度信息将写入TextWriter. 但是,由于此方法可以运行很长时间,因此我想用一些状态信息更新UI.

When I call this method, some progress info is written to the TextWriter. However, since this method can run a long time, I want to update my UI with some status information.

当前,我正在使用StringWriter,但是它没有任何事件.因此,我第一次从StringWriter获得结果是在方法完成之后.

Currently, I am using a StringWriter but it does not have any events. So the first time I get a result from the StringWriter is after the method completed.

现在,我正在搜索从TextWriter继承并触发TextChanged事件或类似事件的类. 我知道这应该不难实现,但是我敢打赌,CLR团队已经为我做到了,我只是找不到合适的课程.

Now I am searching for a class that inherits from TextWriter and fires an TextChanged event or something like that. I know that this shouldn't be to hard to implement, but I bet the CLR Team already did it for me, I just can't find the appropriate class.

推荐答案

如果有人感兴趣,则该类将扩展StringWriter()类以在每次调用writer.Flush()之后触发事件.

If anyone is interested, this is a class that extends the StringWriter() class to fire events after every call to writer.Flush().

我还添加了在每次写入后自动调用Flush()的可能性,因为就我而言,确实写入控制台的第三方组件没有进行刷新.

I also added the posibillity to automatically call Flush() after every write, since, in my case, a third party component, that did write to the console, didn't do a flush.

样品用量:

void DoIt()
{
    var writer = new StringWriterExt(true); // true = AutoFlush
    writer.Flushed += new StringWriterExt.FlushedEventHandler(writer_Flushed);

    TextWriter stdout = Console.Out;
    try
    {
        Console.SetOut(writer);
        CallLongRunningMethodThatDumpsInfoOnConsole();
    }
    finally
    {
        Console.SetOut(stdout);
    }
}

现在,我可以及时显示一些状态信息,而无需等待方法完成.

Now I can display some status information just in time and don't need to wait for the method to finish.

void writer_Flushed(object sender, EventArgs args)
{
    UpdateUi(sender.ToString());
}

这是课程:

public class StringWriterExt : StringWriter
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public delegate void FlushedEventHandler(object sender, EventArgs args);
    public event FlushedEventHandler Flushed;
    public virtual bool AutoFlush { get; set; }

    public StringWriterExt()
        : base() { }

    public StringWriterExt(bool autoFlush)
        : base() { this.AutoFlush = autoFlush; }

    protected void OnFlush()
    {
        var eh = Flushed;
        if (eh != null)
            eh(this, EventArgs.Empty);
    }

    public override void Flush()
    {
        base.Flush();
        OnFlush();
    }

    public override void Write(char value)
    {
        base.Write(value);
        if (AutoFlush) Flush();
    }

    public override void Write(string value)
    {
        base.Write(value);
        if (AutoFlush) Flush();
    }

    public override void Write(char[] buffer, int index, int count)
    {
        base.Write(buffer, index, count);
        if (AutoFlush) Flush();
    }
}

这篇关于如果编写了文本,是否有一个TextWriter子类触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆