By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,587 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,587 IT Pros & Developers. It's quick & easy.

Detect if compiling as a console application

P: n/a

Is there anyway the VB Compiler can detect if a program is a console
application? I have some libraries that need to write to the console if
the program is a console app.

Thanks,
Mike Ober.

May 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
can't you just call System.Console.WriteLine("") anyway? If the app is
a console app, the line will be written to it, otherwise they will not.

May 19 '06 #2

P: n/a
What Chris describes is my experience. I use it alot for throwing
stuff to console that helps me debug. There is a noticable performance
hit though, and I wonder, if the console is not enabled, and a
Console.WriteLine call is made, what happens? Is there some sort of
buffer or resource that the text still writes to? I've never tested
the performance of writing to console when there is no console, as I
only do this when compiled as DEBUG, using conditional compilation
statements.

May 20 '06 #3

P: n/a
> What Chris describes is my experience. I use it alot for throwing
stuff to console that helps me debug. There is a noticable performance
hit though, and I wonder, if the console is not enabled, and a
Console.WriteLine call is made, what happens? Is there some sort of
buffer or resource that the text still writes to? I've never tested
the performance of writing to console when there is no console, as I
only do this when compiled as DEBUG, using conditional compilation
statements.


Maybe System.Windows.Forms.Application.MessageLoop will help decide the
issue at runtime. It returns true if there a message loop is running on the
current thread. So, if your app is single threaded, then it should tell the
difference between a console app and a windows app. Multi-threading
complicates things - a worker thread in a windows app cant effectively use
it. Also, I don't know what it does in other settings, eg a service.

The compiler /target option is what really decides the issue. So the answer
to the original question is yes - the compiler knows about /target. But I
couldn't find out how to access it or an equivalent at compile time (eg via
#if) or run time.
May 20 '06 #4

P: n/a
You get an IOException error if you don't have a console. My current
workaround is as follows:

public sub WriteLog(byval msg as string)
static isConsole as boolean = true
debug.print(msg)
logs.WriteLog(msg) ' Logs is always declared and ready to use
if not isConsole then return
try
Console.Writeline(msg)
catch ex as exception
isConsole = false
end try
end sub

Still a small performance hit (testing a local static variable), but not the
major performance hit everytime it's called.

Mike.
"Snozz" <sh******@cs.fsu.edu> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
What Chris describes is my experience. I use it alot for throwing
stuff to console that helps me debug. There is a noticable performance
hit though, and I wonder, if the console is not enabled, and a
Console.WriteLine call is made, what happens? Is there some sort of
buffer or resource that the text still writes to? I've never tested
the performance of writing to console when there is no console, as I
only do this when compiled as DEBUG, using conditional compilation
statements.


May 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.