[代码]创建,挂起,恢复,结束线程的demo

  1. /*++
  2. Module Name:
  3.     OneThread.cpp
  4. Abstract:
  5.     Create,Start,Suspend,Stop,Destory one thread.
  6. Enviroment:
  7.     Windows NT All Platform;GUI
  8. Note:
  9.     Wanted:
  10.         + Should make a console edition.
  11.         + Should use a function to append string
  12.         + Edit control strange questoin the internal
  13. Revision:
  14.     26-Nov-2008 Created;
  15. Author:
  16.     Benyanwk
  17. --*/
  18. ///
  19. ///DIRECTIVES
  20. //
  21. #define UNICODE
  22. #include <windows.h>
  23. #include "resource.h"
  24. ///FUNCTION DECLARATION
  25. int WINAPI WinMain(
  26.    HINSTANCE hInstance,
  27.    HINSTANCE hPrevInstance,
  28.    LPSTR lpCmdLine,
  29.    int nCmdShow
  30.    );
  31. void DrawNum( void );
  32. INT_PTR DlgProc(
  33.     HWND hwndDlg,
  34.     UINT uMsg,
  35.     WPARAM wParam,
  36.     LPARAM lParam
  37.     );
  38. ///GLOBAL VARIABLE
  39. HWND g_hEdit;
  40. HINSTANCE g_hInst;
  41. DWORD g_dwTid;
  42. HANDLE g_hThread;
  43. ///FUNCTION DEFINTION
  44. ///
  45. int WINAPI WinMain(
  46.    HINSTANCE hInstance,
  47.    HINSTANCE hPervInstance,
  48.    LPSTR lpCmdLine,
  49.    int nCmdShow)
  50. {
  51.     g_hInst = GetModuleHandle( NULL );
  52.     //
  53.     // Create the dialog box
  54.     //
  55.     DialogBoxParam( 
  56.         g_hInst,    // the module instance
  57.         (LPCWSTR)IDD_DIALOG1,   // temple name
  58.         NULL,   // hwnd parent
  59.         (DLGPROC)DlgProc, // the proc name
  60.         NULL  // init dialog parameter
  61.         );
  62.     
  63.     return 0;
  64. }
  65. INT_PTR DlgProc(
  66.     HWND hwndDlg,
  67.     UINT uMsg,
  68.     WPARAM wParam,
  69.     LPARAM lParam
  70.     )
  71. {
  72.     int ndx; // store the edit contorl text length
  73.     HICON hIcon;
  74.     HWND hwndOwner;
  75.     RECT rcDlg,rcOwner,rc;
  76.     switch( uMsg )
  77.     {
  78.     case WM_CLOSE:
  79.         EndDialog( hwndDlg, 0 );
  80.         break;
  81.     case WM_INITDIALOG:
  82.         //
  83.         // Set the icon
  84.         //
  85.         hIcon = LoadIcon( g_hInst, (LPCWSTR)IDI_ICON1 );
  86.         SendMessage( hwndDlg, WM_SETICON, ICON_BIG, (WPARAM)hIcon );
  87.         SendMessage( hwndDlg, WM_SETICON, ICON_SMALL, (WPARAM)hIcon );
  88.         //
  89.         // Centre the dialog
  90.         //
  91.         hwndOwner = GetDesktopWindow();
  92.         GetWindowRect( hwndDlg, &rcDlg );
  93.         GetWindowRect( hwndOwner, &rcOwner );
  94.         CopyRect( &rc, &rcOwner );
  95.         OffsetRect( &rc, -rc.left, -rc.top );
  96.         OffsetRect( &rcDlg, -rcDlg.left, -rcDlg.top );
  97.         OffsetRect( &rc, -rcDlg.right, -rcDlg.bottom );
  98.         
  99.         SetWindowPos(
  100.             hwndDlg,
  101.             HWND_TOP,
  102.             rcOwner.left + ( rc.right / 2),
  103.             rcOwner.top + ( rc.bottom / 2),
  104.             rcDlg.right,
  105.             rcDlg.bottom,
  106.             SWP_NOSIZE
  107.             );
  108.         g_hEdit = GetDlgItem( hwndDlg, IDC_INFO );
  109.         break;
  110.     case WM_COMMAND:
  111.         switch( wParam ) // store the command control ID
  112.         {
  113.         case IDC_CREATE:
  114.             //
  115.             // Draw text before create thread
  116.             //
  117.             SendMessage( g_hEdit, WM_SETTEXT, NULL, (LPARAM)L"+++ 开始创建线程 +++ /n" );
  118.             //
  119.             // Create the thread with CreateThread
  120.             //
  121.             g_hThread = CreateThread(  
  122.                             NULL, // security attributes
  123.                             0,  // using default stack size
  124.                             (LPTHREAD_START_ROUTINE)DrawNum,
  125.                             NULL, // no parameter
  126.                             NULL, // normal create operation
  127.                             &g_dwTid // return the thread id 
  128.                             );
  129.             if( g_hThread == NULL )
  130.             {
  131.                 //
  132.                 // indicate CreateThread failed!
  133.                 //
  134.                 MessageBox( 
  135.                     0,
  136.                     L"不能够创建线程",
  137.                     0,
  138.                     0 );
  139.                 ExitProcess( 0 );
  140.             }
  141.             //
  142.             // Append the string after string
  143.             //
  144.             ndx = GetWindowTextLength( g_hEdit );
  145.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );    
  146.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+++ 创建线程结束 +++/n" );
  147.             break;
  148.         
  149.         case IDC_SUSPEND:
  150.             
  151.             //
  152.             // Append the string
  153.             //
  154.             ndx = GetWindowTextLength( g_hEdit );
  155.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  156.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 正在准备挂起线程/n" );
  157.             //
  158.             // Suspend the created thread
  159.             //
  160.             SuspendThread( g_hThread );
  161.             
  162.             //
  163.             // Append another string
  164.             //
  165.             ndx = GetWindowTextLength( g_hEdit );
  166.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  167.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功挂起线程 /n" );
  168.             break;
  169.         case IDC_RESUME:
  170.             //
  171.             // Append the string
  172.             //
  173.             ndx = GetWindowTextLength( g_hEdit );
  174.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  175.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 正在准备恢复线程/n" );
  176.             //
  177.             // Resume the created thread
  178.             //
  179.             ResumeThread( g_hThread );
  180.             //
  181.             // Append the string
  182.             //
  183.             ndx = GetWindowTextLength( g_hEdit );
  184.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  185.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功恢复线程/n" );
  186.             break;
  187.         
  188.         case IDC_TERMINATE:
  189.             
  190.             //
  191.             // Append the string
  192.             //
  193.             ndx = GetWindowTextLength( g_hEdit );
  194.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  195.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 准备结束线程/n" );
  196.             //
  197.             // Terminate the thread
  198.             //
  199.             TerminateThread( 
  200.                 g_hThread,
  201.                 1 //indicate not normal terminated
  202.                 );
  203.             // 
  204.             // Append the string
  205.             //
  206.             ndx = GetWindowTextLength( g_hEdit );
  207.             SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  208.             SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)L"+ 成功结束线程/n" );
  209.             break;
  210.         case IDC_EXIT:
  211.             //
  212.             // indicate exit
  213.             //
  214.             EndDialog( hwndDlg, 0 );
  215.             break;
  216.             
  217.         default:
  218.             return FALSE; // indicate not procceed 
  219.         }
  220.     case WM_SIZE:
  221.         
  222.         // ignore
  223.     default:
  224.         return FALSE; // indicate not procceed
  225.     }
  226. }
  227. void DrawNum()
  228. /*++
  229. Routine Name:
  230.     DrawNum
  231. Description:
  232.     Draw numbers from 0 to infinity
  233. --*/
  234. {
  235.     int i = 0;
  236.     int ndx;
  237.     WCHAR temp[100]; // actually only 500 bit
  238.     WCHAR temp2[100];
  239.     
  240.     //
  241.     // Append the number
  242.     //
  243. loop:
  244.     ndx = GetWindowTextLength( g_hEdit );
  245.     SendMessage( g_hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx );
  246.     if( i%5 == 0 )
  247.         wsprintf( temp, L"%d/n", i++ );
  248.     else
  249.         wsprintf( temp, L"%d/t", i++ );
  250.     SendMessage( g_hEdit, EM_REPLACESEL, 0, (LPARAM)temp );
  251.     goto loop;
  252. }

相应的工程文件:http://www.live-share.com/files/368628/C2_ThreadDemo.rar.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值