TargetConsole constant needed

When writing code that can be reused in both desktop and console apps, pausing the app requires different methods, either app.SleepCurrentThread or app.DoEvents. It would be very convenient to write

#if TargetConsole app.DoEvents(100) #else app.sleepCurrentThread(100) #endif
instead of having separate apps or having both lines of code and remembering to comment out one of them.

If you agree, add Feedback #33622 to your favorites.

It is called TargetHasGUI.

Another cool way of doing it is using the Attributes on a method, Console, Web, GUI. Write two methods on your App class, mark one for console only and the other for GUI only. Then when you want to use it, just do:

App.SleepOrEvents(100)

Or whatever you wound up calling it. This method is nice when you wind up calling that conditional code in various places in your application.

I will agree with Dean, that TargetHasGUI is a little out of place with the other Target friends. Other Target friends are “Is this our target?” For example, we have Target32Bits AND Target64Bits. We don’t use Not Target32Bits to determine if we are on a 64bit system. TargetConsole, TargetDesktop and the already existing TargetWeb would be much easier, consistent and clearer in code. It would also match with the Attribute switches, Console, Desktop and Web.

I know it’s a little muddy, because TargetHasGUI returns False for web apps, when it could be argued (easily) that they do have a GUI. TargetConsole could potentially return True for web apps, since web apps are console apps.

Besides, it’s not always that simple. In Dean’s case, the code could be written as

#if TargetHasGUI App.SleepCurrentThread(100) #else App.DoEvents(100) #endif

and it would compile. But it would be wrong, because DoEvents in a web app should never be used. So you would write it as

#if TargetHasGUI App.SleepCurrentThread(100) #else #if TargetWeb // Nothing you can do #else App.DoEvents(100) #endif #endif

and it would kind of be better, but you’d still need to be sure not to call this method from code executing within an event loop. And on a web app, there is nothing you could intelligently.

But I’m getting off topic. My point is it isn’t always simple.

[quote=90087:@Jeremy Cowgar]Another cool way of doing it is using the Attributes on a method, Console, Web, GUI. Write two methods on your App class, mark one for console only and the other for GUI only. Then when you want to use it, just do:
[/quote]
Those are the “compatibility flags”