473,597 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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 37723
Plater
7,872 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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 New Member
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 New Member
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
578
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 database "db_owner" for master database. The problem is that when this user opens up any job (i.e. owned by any user) in the SQL Server Agent, and goes to the Notifications tab, the first three alerts (Email, Page and Net send) are greyed out, i.e.
11
24043
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, the handler will be executed. I tried using a while(timer.Enabled) {} loop but it made the process use 90% cpu in the task manager. What is the correct way to wait for an event and keep the program alive in a console application? Thanks, Aviv.
2
2941
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 'params' there? I am used to this pattern: type var, type var, type var etc... like: string str, int i, double x. This console.writeline overload says 'params object arg' ? 2) I noticed that the console.writeline( string, object, object, object,
5
12016
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 show star(*) when entering?
10
6320
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 so that the window for the Console application is not visible. However, when using some of the 'advanced' properties of the StartInfo object, like Username, Password and Domain, the WindowsStyle property of the StartInfo object is ignored....
3
2289
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 Sub Main() Dim instr As String = Command()
15
2780
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 be a windows app. If I make it a windows app then I can't write to the console. If I make it a console app then it starts a console if the user runs it from the start menu so from what I see it's not possible. Thanks in advance, Michael
0
1728
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 when you convert the varchar column to text column. Do this through Enterprise Manager Console Create a New table with a column as varchar datatype from Enterprise Manager  table created  Open the table and add one row  Successfully added the...
5
3494
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 doing so. does anyone know how? thanks so much
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.