Console I/o plus CreateThread

Source: Internet
Author: User

Console I/o plus CreateThread
/*demonsttrates How to write a program this CNA use
CreateThread instead of calling _beginthreadex.
This program does not need the multithread library.
Could use Readconsole and writeconsole.
There is minor but significant differences between thest
Functions and ReadFile and WriteFile
This was ANSI only, it would not be compile for Unicode.
*/
#define Win32_lean_and_mean
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>//to init rand ()
#include "MtVerify.h"


/***************************/
/*constants*/
#define MAX_THREADS 256
#define INPUT_BUF_SIZE 80
#define BANNER_SIZE 12
#define Output_text_color Background_blue |\
foreground_red | Foreground_green | Foreground_blue
/******************************
*function prototypes
*/
void Mainloop (void);
void Clearscreen (void);
void shutdownthreads (void);
void Prompt (LPCSTR str);//display title bar Info
int STRIPCR (LPSTR buf);
/*thread Startup function*/
DWORD WINAPI Bannerproc (LPVOID pparam);
/*************************************
* Global variables*/
HANDLE Hconsolein;
HANDLE hconsoleout;
HANDLE Hrunobject;
HANDLE Threadhandles[max_threads];
int Nthreads;//number of Threads started


Console_screen_buffer_info Csbiinfo;
/******************************************
*stucture passed to thread on startup */
typedef struct
{
TCHAR Buf[input_buf_size];
Short x;
Short y;
} DataBlock;


/***************************************************
Primary thread enters here*/
int main ()
{
/*get Display screen information &clear the screen*/
Hconsolein = GetStdHandle (Std_input_handle);
Hconsoleout = GetStdHandle (Std_output_handle);
Getconsolescreenbufferinfo (Hconsoleout, &csbiinfo);
Clearscreen ();
/*create the event object that keeps threads running*/
Mtverify (hrunobject = CreateEvent (null, TRUE, 0, null));
/*start waiting for keyboard input to dispatch threads or exit*/
Mainloop ();
Shutdownthreads ();
Clearscreen ();
CloseHandle (Hrunobject);
CloseHandle (Hconsolein);
CloseHandle (hconsoleout);
return exit_success;
}


void Shutdownthreads (void)
{
if (Nthreads > 0)
{
/*since This is a manual event,all threads would be
Woken up at once*/
Mtverify (SetEvent (hrunobject));
Mtverify (WaitForMultipleObjects (Nthreads,
Threadhandles, TRUE, INFINITE)! = wait_failed);
while (--nthreads)
{
Mtverify (CloseHandle (threadhandles[nthreads));
}
}
}
/*dispatch and Count threads*/
void Mainloop (void)
{
TCHAR Buf[input_buf_size];
DWORD Bytesread;
DataBlock *data_block;
DWORD thread_id;
Srand (Time (NULL));
for (;;)
{
Prompt ("Type string to display or ENTER to exit:");
Mtverify (ReadFile (Hconsolein, buf, Input_buf_size-1,
&bytesread, NULL));
/*readfile is Binary,not line oriented,so terminate the string*/
Buf[bytesread] = ' + ';
Mtverify (Flushconsoleinputbuffer (Hconsolein));
if (STRIPCR (buf) = = 0)
Break
if (Nthreads < max_threads)
{
Data_block = HeapAlloc (GetProcessHeap (), heap_zero_memory, sizeof (DataBlock));
strcpy (Data_block->buf, buf);
Data_block->x = rand () * (csbiinfo.dwsize.x-banner_size)/Rand_max;
Data_block_->y = rand () * (csbiinfo.dwsize.y-1)/Rand_max + 1;
Mtverify (threadhandles[nthreads++] = CreateThread (
NULL, 0, Bannerproc, data_block, 0, &thread_id));
}
}
}
void Clearscreen (void)
{
DWORD dummy;
COORD Home = {0, 0};
Fillconsoleoutputattribute (Hconsoleout,csbiinfo.wattributes,
Csbiinfo.dwsize.x*csbiinfo.dwsize.y,home,&dummy);
Fillconsoleoutputcharacter (Hconsoleout, ",
Csbiinfo.dwsize.x*csbiinfo.dwsize.y, Home, &dummy);
}
void Prompt (LPCSTR str)
{
COORD Home = {0,0};
DWORD dummy;
int len = strlen (str);
SetConsoleCursorPosition (Hconsoleout, Home);
WriteFile (Hconsoleout, str, Len, &dummy, FALSE);
home.x = Len;
Fillconsoleoutputcharacter (Hconsoleout, ",
Csbiinfo.dwsize.x-len, Home, &dummy);
}
/*********************************
Routines from here-down is used only by worker threads*/
DWORD WINAPI Bannerproc (LPVOID pparam)
{
DataBlock *thread_data_block = Pparam;
COORD TopLeft = {0, 0};
COORD Size = {banner_size, 1};
int I, J;
int Len;
int scrollposition = 0;
TCHAR outputbuf[input_buf_size + banner_size];
Char_info charbuf[input_buf_size + banner_size];
Small_rect RECT;


Rect. left = thread_data_block->x;
Rect. right = rect. Left + banner_size;
Rect. Top = thread_data_block->y;
Rect. Bottom = rect. Top;
strcpy (Outputbuf, thread_data_block->buf);
Len = strlen (OUTPUTBUF);
for (i = len; i < banner_size; i++)
Outputbuf[i] = ";
if (Len < banner_size) len = banner_size;
strncpy (Outputbuf + len, outputbuf, banner_size);
Outputbuf[len + banner_size-1] = ' + ';
Mtverify (HeapFree (GetProcessHeap (), 0, Pparam));
Do
{
for (i = scrollposition++, j = 0; j < Banner_size; i++, J + +)
{
CHARBUF[J]. Char.asciichar = Outputbuf[i];
CHARBUF[J]. Attributes = Output_text_color;
}
if (scrollposition = = len)
scrollposition = 0;
Mtverify (Writeconsoleoutput (Hconsoleout, Charbuf, Size, TopLeft, &rect));
} while (WaitForSingleObject (hrunobject, 125L) = = Wait_timeout);
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Console I/o plus createthread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.