Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads in a console app

Posted on 1998-05-23
1
Medium Priority
?
311 Views
Last Modified: 2013-11-20
How does one "fork" a new process using MFC in a console app? I know the call is AfxStartThread(), but all the examples I can find are Windows based!!! Code sample would be greatly appreciated.

andy t.
0
Comment
Question by:tudhopea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1314484
The Windows Win32 API doesn't provide fork() so you can't do it that way.  Use the C-runtime function _beginthread().  On it's man page in the SDK documentation, you will find the following example:

/* BEGTHRD.C illustrates multiple threads using functions: *
 *      _beginthread            _endthread * *
 * This program requires the multithreaded library. For example,
 * compile with the following command line: *     CL /MT /D "_X86_" BEGTHRD.C *
 * If you are using the Visual C++ development environment, select the
 * Multi-Threaded runtime library in the compiler Project Settings
 * dialog box. *  */#include <windows.h>
#include <process.h>    /* _beginthread, _endthread */#include <stddef.h>
#include <stdlib.h>#include <conio.h>void Bounce( void *ch );
void CheckKey( void *dummy );
/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
BOOL repeat = TRUE;     /* Global repeat flag and video variable */
HANDLE hStdOut;         /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */
void main(){    CHAR    ch = 'A';
    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    /* Get display screen's text row and column information. */
   GetConsoleScreenBufferInfo( hStdOut, &csbi );
    /* Launch CheckKey thread to check for terminating keystroke. */
    _beginthread( CheckKey, 0, NULL );
    /* Loop until CheckKey terminates program. */    while( repeat )    {
        /* On first loops, launch character threads. */
        _beginthread( Bounce, 0, (void *) (ch++)  );
        /* Wait one second between loops. */        Sleep( 1000L );    }}
/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy ){    _getch();
    repeat = 0;    /* _endthread implied */}
/* Bounce - Thread to create and and control a colored letter that moves
 * around on the screen. * * Params: ch - the letter to be moved */
void Bounce( void *ch ){
    /* Generate letter and color attribute from thread argument. */
    char    blankcell = 0x20;    char    blockcell = (char) ch;
    BOOL    first = TRUE;   COORD   oldcoord, newcoord;   DWORD   result;
    /* Seed random number generator and get initial location. */
    srand( _threadid );    newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
    newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );    while( repeat )    {
        /* Pause between loops. */        Sleep( 100L );
        /* Blank out our old position on the screen, and draw new letter. */
        if( first )            first = FALSE;        else
         WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
         WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );
        /* Increment the coordinate for next placement of the block. */
        oldcoord.X = newcoord.X;        oldcoord.Y = newcoord.Y;
        newcoord.X += GetRandom( -1, 1 );
        newcoord.Y += GetRandom( -1, 1 );
        /* Correct placement (and beep) if about to go off the screen. */
        if( newcoord.X < 0 )            newcoord.X = 1;
        else if( newcoord.X == csbi.dwSize.X )
            newcoord.X = csbi.dwSize.X - 2;        else if( newcoord.Y < 0 )
            newcoord.Y = 1;        else if( newcoord.Y == csbi.dwSize.Y )
            newcoord.Y = csbi.dwSize.Y - 2;
        /* If not at a screen border, continue, otherwise beep. */        else
            continue;        Beep( ((char) ch - 'A') * 100, 175 );    }
    /* _endthread given to terminate */    _endthread();}
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question