Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

constructing console type app

Status
Not open for further replies.

ugly

Programmer
Jul 5, 2002
70
GB
I want to construct a console type application that: takes a large array of hard coded strings ie call them str1, str2 , str3 etc. When the program starts I want the strings ouput to a console window sequentially with a variable step period greater of 5 seconds or more between the display of each string.
Other considerations are that as each string appears the previous disappears,such that only one string is displayed at a particular time, I dont want the command prompt to appear or any task or menu bars visible, it just has to be the string in a totally empty console window that occupies the whole of the screen. I will also need a way of exiting the app. I would be most grateful if anyone could help me to construct this. This prog will form the basis for a art project that I am involved in ---ta
 
Hmmm. You are using Visual Studio, yes? Yse the app wizard to create a console app for you.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
There are many solution for that.
One solution is to create two threads:
-one thread will watch if the user press a specific key to exit the application. When this happens the whole console application terminates
-one thread is clearing the output screen and will display the next string with a given delay. This thread will finish when all strings where displayed or a request to exit occured (set by the watching thread).

I put here an ordinary example of the above explanation (two threads) that will display 5 different strings on the console , with a delay between of 5 seconds while all strings are displayed or the user press the q key in order to exit the application.
The both threads and the main program exchange a common structure to pass data at run time.
You should compile and link with one of the multithreaded C run-time libraries.

//#include &quot;stdafx.h&quot;

#include <stdio.h>
#include <conio.h>

#include <process.h>
#include <afxmt.h>
#include <AFXPRIV.H>

long DELAY = 5000L;
struct Param
{
Param(int size)
{
m_bStop = false;
m_Size = size;
};
BOOL m_bStop;
int m_Size;
};


void ThreadRoute1( void* arg )
{
CString sTemp;
int Count =0;
while (Count < ((Param*)arg)->m_Size)
{

system(&quot;cls&quot;);
sTemp.Format(&quot;%d: String %d&quot;, GetTickCount(), Count);
Count++;
printf(&quot;%s\n&quot;, sTemp);
Sleep(DELAY);
if (((Param*)arg)->m_bStop )
break;
}
((Param*)arg)->m_bStop = true;
_endthread();

};
void ThreadRoute2(void * arg)
{

int ch;
while (true)
{
ch = _getch();
ch = toupper( ch );
if (ch =='Q')
{
((Param *)arg)->m_bStop = true;
break;
}
}
_endthread();
}
int main(int argc, char* argv[])
{
Param *pStruct = new Param(5); // 5 strings
_beginthread( ThreadRoute2, 0, (void*)pStruct );
_beginthread( ThreadRoute1, 0, (void*)pStruct );
while (true)
{
if (pStruct->m_bStop)
break;
Sleep(100L);

}
return 0;
}

Improvments for this example:
Put the code to access the hard and huges strings to be displayed.
Set automatically the m_Size depending on the number of strings.
Get the delay parameter as parameter from the command line

Another solution:

Create two processes that share the same console: one will display the strings and another one will trap the exit action from the user.
Use GenerateConsoleCtrlEvent() to send a specified signal (CTRL+c, CTRL+break) to the both processes


-obislavu-



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top