- /*++
- Module Name:
- OneThread.cpp
- Abstract:
- Create,Start,Suspend,Stop,Destory one thread.
- Enviroment:
- Windows NT All Platform;GUI
- Note:
- Wanted:
- + Should make a console edition.
- + Should use a function to append string
- + Edit control strange questoin the internal
- Revision:
- 26-Nov-2008 Created;
- Author:
- Benyanwk
- --*/
- ///
- ///DIRECTIVES
- //
- #define UNICODE
- #include <windows.h>
- #include "resource.h"
- ///FUNCTION DECLARATION
- int WINAPI WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow
- );
- void DrawNum( void );
- INT_PTR DlgProc(
- HWND hwndDlg,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam
- );
- ///GLOBAL VARIABLE
- HWND g_hEdit;
- HINSTANCE g_hInst;
- DWORD g_dwTid;
- HANDLE g_hThread;
- ///FUNCTION DEFINTION
- ///
- int WINAPI WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPervInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- g_hInst = GetModuleHandle( NULL );
- //
- // Create the dialog box
- //
- DialogBoxParam(
- g_hInst, // the module instance
- (LPCWSTR)IDD_DIALOG1, // temple name
- NULL, // hwnd parent
- (DLGPROC)DlgProc, // the proc name
- NULL // init dialog parameter
- );
- return 0;
- }
- INT_PTR DlgProc(
- HWND hwndDlg,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam
- )
- {
- int ndx; // store the edit contorl text length
- HICON hIcon;
- HWND hwndOwner;
- RECT rcDlg,rcOwner,rc;
- switch( uMsg )
- {
- case WM_CLOSE:
- EndDialog( hwndDlg, 0 );
- break;
- case WM_INITDIALOG:
- //
- // Set the icon
- //
- hIcon = LoadIcon( g_hInst, (LPCWSTR)IDI_ICON1 );
- SendMessage( hwndDlg, WM_SETICON, ICON_BIG, (WPARAM)hIcon );
- SendMessage( hwndDlg, WM_SETICON, ICON_SMALL, (WPARAM)hIcon );
- //
- // Centre the dialog
- //
- hwndOwner = GetDesktopWindow();
- GetWindowRect( hwndDlg, &rcDlg );
- GetWindowRect( hwndOwner, &rcOwner );
- CopyRect( &rc, &rcOwner );
- OffsetRect( &rc, -rc.left, -rc.top );
- OffsetRect( &rcDlg, -rcDlg.left, -rcDlg.top );
- OffsetRect( &rc, -rcDlg.right, -rcDlg.bottom );
- SetWindowPos(
- hwndDlg,
- HWND_TOP,
- rcOwner.left + ( rc.right / 2),
- rcOwner.top + ( rc.bottom / 2),
- rcDlg.right,
- rcDlg.bottom,
- SWP_NOSIZE
- );
- g_hEdit = GetDlgItem( hwndDlg, IDC_INFO );
- break;
- case WM_COMMAND:
- switch( wParam ) // store the command control ID
- {
- case IDC_CREATE:
- //
- // Draw text before create thread
- //
- SendMessage( g_hEdit, WM_SETTEXT, NULL, (LPARAM)L"+++ 开始创建线程 +++ /n" );
- //
- // Create the thread with CreateThread
- //
- g_hThread = CreateThread(
- NULL, // security attributes
- 0, // using default stack size
- (LPTHREAD_START_ROUTINE)DrawNum,
- NULL, // no parameter
- NULL, // normal create operation
- &g_dwTid // return the thread id
- );
- if( g_hThread == NULL )
- {
- //
- // indicate CreateThread failed!
- //
- MessageBox(
- 0,
- L"不能够创建线程",
- 0,
- 0 );
- ExitProcess( 0 );
- }
- //
- // Append the string after string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+++ 创建线程结束 +++/n" );
- break;
- case IDC_SUSPEND:
- //
- // Append the string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 正在准备挂起线程/n" );
- //
- // Suspend the created thread
- //
- SuspendThread( g_hThread );
- //
- // Append another string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功挂起线程 /n" );
- break;
- case IDC_RESUME:
- //
- // Append the string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 正在准备恢复线程/n" );
- //
- // Resume the created thread
- //
- ResumeThread( g_hThread );
- //
- // Append the string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功恢复线程/n" );
- break;
- case IDC_TERMINATE:
- //
- // Append the string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 准备结束线程/n" );
- //
- // Terminate the thread
- //
- TerminateThread(
- g_hThread,
- 1 //indicate not normal terminated
- );
- //
- // Append the string
- //
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功结束线程/n" );
- break;
- case IDC_EXIT:
- //
- // indicate exit
- //
- EndDialog( hwndDlg, 0 );
- break;
- default:
- return FALSE; // indicate not procceed
- }
- case WM_SIZE:
- // ignore
- default:
- return FALSE; // indicate not procceed
- }
- }
- void DrawNum()
- /*++
- Routine Name:
- DrawNum
- Description:
- Draw numbers from 0 to infinity
- --*/
- {
- int i = 0;
- int ndx;
- WCHAR temp[100]; // actually only 500 bit
- WCHAR temp2[100];
- //
- // Append the number
- //
- loop:
- ndx = GetWindowTextLength( g_hEdit );
- SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
- if( i%5 == 0 )
- wsprintf( temp, L"%d/n", i++ );
- else
- wsprintf( temp, L"%d/t", i++ );
- SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)temp );
- goto loop;
- }
相应的工程文件:http://www.live-share.com/files/368628/C2_ThreadDemo.rar.html