From: Peter Duniho on
shapper wrote:
> Hi,
>
> I am using the TextWriterProxy in the WPF application as follows:
>
> TextBlock output = new TextBlock();
>
> TextWriterProxy proxy = new TextWriterProxy();
> proxy.Add(Console.Out);
>
> StringBuilder sb = new StringBuilder();
> StringWriter resultStringWriter = new StringWriter(sb);
> proxy.Add(resultStringWriter);
> output.Text = sb.ToString();
>
> However, text block does not show any text. Any idea how to do this
> integration?

Not without a concise-but-complete code example.

The code you posted does not in and of itself show anything that should
necessarily add text to your TextBlock class. The StringWriter you've
added to your proxy isn't going to have any text in it until you
actually write some text to the proxy. In the code you show, the only
time you set the Text property of your TextBlock is immediately after
initializing the proxy, before you've actually written anything to it.

It seems to me that if you want immediate updates in your TextBlock
according to writes to the proxy, you need a custom TextWriter (in
addition to the proxy, or just make the proxy understand the TextBlock
instance specifically) that when written to, it updates the TextBlock
accordingly (by adding the text to the TextBlock's current text).

Pete
From: shapper on
On Nov 20, 6:41 pm, Peter Duniho <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> Not without a concise-but-complete code example.
>
> The code you posted does not in and of itself show anything that should
> necessarily add text to your TextBlock class.  The StringWriter you've
> added to your proxy isn't going to have any text in it until you
> actually write some text to the proxy.  

Sorry, I am very familiar with WPF and console.
I would create an full code example if I could replicate what I am
doing.

I have the following example which hopefully will explain what I am
trying to do:

DockPanel content = new DockPanel();
TextBlock output = new TextBlock();
TextWriterProxy proxy = new TextWriterProxy();
proxy.Add(Console.Out);

StringBuilder sb = new StringBuilder();
StringWriter resultStringWriter = new StringWriter(sb);
proxy.Add(resultStringWriter);

proxy.WriteLine("Start");
PackService.Run();
proxy.WriteLine("Finish");
output.Text = sb.ToString();

DockPanel.SetDock(output, Dock.Bottom);
content.Children.Add(output);
Content = content;

PackService.Run() minimizes some CSS and JS code. So in console I get:

Start
Writing output to C:\Users\Miguel\Projects\WCA.Presentation\Scripts
\WCA.Site.min.js
Writing output to C:\Users\Miguel\Projects\WCA.Presentation\Scripts
\JQuery-1.3.2.Plugins.min.js
Writing output to C:\Users\Miguel\Projects\WCA.Presentation\Styles
\WCA.Base.min.css
Finish

I would like to "see" this on my Text Block but I get only:
Start
Finish

In fact I would like the TextBlock to match what is shown in console
and in every part of my code, for example, even inside PackService.Run
() to be able to send something to console that would also show in
TextBlock.

Am I explaining this correctly?

Thank You,
Miguel






From: shapper on
I took another small step to solve this:

Console.SetOut(proxy);
proxy.WriteLine("Start");
PackService.Run();
proxy.WriteLine("Finish");
output.Text = sb.ToString();

Now I "see" all the lines on my TextBlock.

The problem is that if I click a button on my WPF menu that calls
PackService.Run() in this case nothing happens on the TextBox.

My idea would make all my application aware of the proxy and that
SetOut(proxy) ... Is this possible?
From: Peter Duniho on
shapper wrote:
> [...]
> In fact I would like the TextBlock to match what is shown in console
> and in every part of my code, for example, even inside PackService.Run
> () to be able to send something to console that would also show in
> TextBlock.
>
> Am I explaining this correctly?

I don't know. As I said, a concise-but-complete code example would go a
long way to improving your question.

That said, if I understand correctly, the problem is that your
PackService.Run() method is still writing directly to the Console class.
It should use your proxy class instead.

Though, that said, your original choice to redirect the Console standard
output by calling SetOut() should also work, and wouldn't require
changes to the PackService.Run() method. Just because my original
suggestion was different from what you thought I meant, or different
from some other approach that might work, that doesn't mean you have to
follow my original suggestion.

Beyond that, it's really impossible to provide specific advice without a
specific question.

Pete
From: Peter Duniho on
shapper wrote:
> [...]
> My idea would make all my application aware of the proxy and that
> SetOut(proxy) ... Is this possible?

Just create the proxy at the beginning of your program, and call
Console.SetOut() with the proxy before any of the stuff happens for
which you want output.