473,230 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

C#-ConsoleApp: How to turn off console notifications.

7
Hello
I developing console application that sometimes writes some information to console using Console.WriteLn or Console.Write.
Is it possible to turn on/off displaying of messages that will be written out into console?

Sometimes I don't want anything to be written in the console and sometimes I need it. I hope someone knows solution to this.

thanks
AkipNG
Jun 1 '07 #1
6 37469
Plater
7,872 Expert 4TB
comment out the WriteLn() and Write() calls?

or

put them in a wrapper function that has logic to know if it should or should not write to the console:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void WriteLine(string msg)
  3. {
  4.    if (ShouldWrite)
  5.    {
  6.       Console.WriteLn(msg);
  7.    }
  8. }
  9.  
Jun 1 '07 #2
akipng
7
comment out the WriteLn() and Write() calls?

or

put them in a wrapper function that has logic to know if it should or should not write to the console:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void WriteLine(string msg)
  3. {
  4.    if (ShouldWrite)
  5.    {
  6.       Console.WriteLn(msg);
  7.    }
  8. }
  9.  

Thank you for input.
This is not solution I was looking for. Some of the dll's I use generates console output and I can't use wrapper method on in that libraries.

Is there an other way to turn off console? Something like ECHO OFF in old dos scripts?

thanks
AkipNG
Jun 1 '07 #3
Plater
7,872 Expert 4TB
You can redirec the ouput stream to elsewhere with Console.SetOut() (and also .SetIn() and SetError())
Give a quick check in MSDN on them, should accomplish what you're looking for.
Jun 1 '07 #4
akipng
7
You can redirec the ouput stream to elsewhere with Console.SetOut() (and also .SetIn() and SetError())
Give a quick check in MSDN on them, should accomplish what you're looking for.
Great!
That worked out. I saved current console 'Console.Out' in 'saveOut' variable and when I need to call a Console command I switch it and post it then switch back to TextWriter.Null. So no other text is spamming console.

Expand|Select|Wrap|Line Numbers
  1. private TextWriter saveOut=null;
  2. public void WriteToConsole(messageToConsole)
  3. {
  4. if (saveOut==null) // first time use
  5.                 saveOut = Console.Out;
  6.             Console.SetOut(saveOut); 
  7.             Console.WriteLine(messageToConsole);
  8.             Console.SetOut(TextWriter.Null);
  9. }
  10.  
Thank you :)
AkipNG
Jun 1 '07 #5
akipng
7
The solution provided above only works when application uses Console static object to write messages.
Unfortunately I use library (dll) that still writes messages to console, I believe that library is written in c++ and uses system calls to do that.
Anyway to stop it? maybe intercept the message in WndProc that cause the console message placement, how?

thanks
AkipNG
Jun 1 '07 #6
RBGames
1 Bit
Was googling how to do this and found this post. Using this info and some other posts I put this together. Maybe someone else will land here and find this useful. BTW, this is C# 10.0.

Expand|Select|Wrap|Line Numbers
  1. public static class ConsoleEx
  2. {
  3.     private static TextWriter? defaultConsoleOut = null;
  4.  
  5.     public static void SetEcho(bool flag)
  6.     {
  7.         if (defaultConsoleOut == null) defaultConsoleOut = Console.Out;
  8.  
  9.         if (flag)
  10.         {
  11.             Console.SetOut(defaultConsoleOut);
  12.         }
  13.         else
  14.         {
  15.             Console.SetOut(TextWriter.Null);
  16.         }
  17.     }
  18. }
  19.  
Usage would be:
Expand|Select|Wrap|Line Numbers
  1. // turn console output off
  2. ConsoleEx.SetEcho(false);
  3.  
  4. // turn console output on
  5. ConsoleEx.SetEcho(true);
  6.  
Jan 6 '22 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dipak Patel | last post by:
Platform: Win2000 SP3, SQL 7.00 - 7.00.1063 I have a SQL-authenticated user with the following permissions: "Process Administrators" server role "db_owner" and "TargetServersRole" for msdb...
11
by: avivgur | last post by:
Hello, I have devised a console application that uses a System.Timers.Timer and its Elapsed event. The problem is that I want the program to continue to run so that each time the event is raised,...
2
by: djc | last post by:
out of all the overloads that pop up via intellisense for console.writeline() the following one confuses me: Console.WriteLine (string format, params object arg) 1) whats the deal with...
5
by: Hooyoo | last post by:
Hi, here. I write following codes: string password = Console.ReadLine(); I want users enter their passwords, but readline will show content of password when entering, so is there any way to...
10
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
3
by: jim | last post by:
VB.net code to turn monitor on and off....... <CODE> Module Module1 Const HWND_BROADCAST As Integer = &HFFFF Const SC_MONITORPOWER As Integer = &HF170 Const WM_SYSCOMMAND As Short = &H112S ...
15
by: Michael C | last post by:
Is it possible to have an app that is both a console app and a windows app? If there are no command line switches then it will run as a console app, if there are command line switches then it will...
0
Krishna Ladwa
by: Krishna Ladwa | last post by:
In Sql Server 2000 Version, I found that no Notification message box appears when converting text column to varchar but the data gets truncated to the given size for the varchar. Whereas it appears...
5
by: njuneardave | last post by:
I have a full-screen C# app, but windows notifications (the little balloons on the bottom right-hand of the screen) and outlook reminders will pop on top of my app. i want to prevent them from...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.