views:

68

answers:

1

I need to call a 3rd party library that happens to spew a bunch of stuff to the console. The code simply like this...

int MyMethod(int a)
{
   int b = ThirdPartyLibrary.Transform(a);  // spews unwanted console output
   return b;
}

Is there an easy way to supress the unwanted console output from ThirdPartyLibrary? For performance reasons, new processes or threads cannot be used in the solution.

+2  A: 

Well you can use Console.SetOut to an implementation of TextWriter which doesn't write anywhere:

Console.SetOut(TextWriter.Null);

That will suppress all console output though. You could always maintain a reference to the original Console.Out writer and use that for your own output.

Jon Skeet
This does work in most cases. However, in my case it does not. The external library creates a COM object and at least 1 other thread - possibly even another process so that is complicating my problem.
RichAmberale
Ah. It would have been nice to have known that originally... if it's creating other processes or writing the the console in ways other than using `Console.WriteLine` etc, that does make things a lot harder...
Jon Skeet
Yes - sorry, I didn't realize that was important until trying your suggestion.
RichAmberale