lyfztestodbc1.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // lyfztestodbc1.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "lyfztestodbc1.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. // 唯一的应用程序对象
  9. CWinApp theApp;
  10. using namespace std;
  11. volatile LONG nThreadCount = 0;
  12. //unsigned __stdcall ThreadWork( LPVOID lpInput )
  13. DWORD WINAPI ThreadWork( LPVOID lpInput )
  14. {
  15. IODBCPoolInterface *pInstance = (IODBCPoolInterface *)lpInput;
  16. DWORD dwThreadId = GetCurrentThreadId();
  17. CArray<CStringArray,CStringArray> Ary;
  18. //pInstance->GetOrderInfo( &Ary , 5000);
  19. pInstance->GetEnterprisInfo(_T("多多福"), Ary);
  20. //Sleep(10);
  21. InterlockedIncrement(&nThreadCount);
  22. // printf("Thread Finish Count = %d\n", nThreadCount);
  23. printf("log[%d]:\t %d\n",dwThreadId,nThreadCount);
  24. //
  25. for ( int i = 0; i < Ary.GetSize(); i++ )
  26. {
  27. CStringArray &strAry = Ary.ElementAt(i);
  28. for ( int n = 0; n < strAry.GetSize(); n++ )
  29. {
  30. printf("log[%d]: %s\n",dwThreadId, strAry.ElementAt(n));
  31. }
  32. }
  33. return 0;
  34. }
  35. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  36. {
  37. int nRetCode = 0;
  38. // 初始化 MFC 并在失败时显示错误
  39. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  40. {
  41. // TODO: 更改错误代码以符合您的需要
  42. _tprintf(_T("错误: MFC 初始化失败\n"));
  43. nRetCode = 1;
  44. }
  45. else
  46. {
  47. // TODO: 在此处为应用程序的行为编写代码。
  48. //////////////////////////////////////////////////////////////////////////
  49. // 测试用例;
  50. IODBCPoolInterface *pInstance = lyfzCreatePoolInstance();
  51. if ( pInstance == NULL )
  52. {
  53. printf("log:打开数据库连接池失败\n");
  54. return -1;
  55. }
  56. /*
  57. DWORD dwCount = pInstance->InitializePool(_T("IT-SERVER\\SQLEXPRESS"),
  58. 0,
  59. _T("sa"),
  60. _T("ly1234"),
  61. _T("219"),
  62. 5,
  63. 20);
  64. */
  65. DWORD dwCount = pInstance->InitializePool(_T("192.168.1.223"),
  66. 1011,
  67. _T("sa"),
  68. _T("ly1234"),
  69. _T("dbmsg"),
  70. 5,
  71. 20);
  72. if ( dwCount == 0 )
  73. {
  74. printf("log:创建连接对象0个,退出\n");
  75. return -1;
  76. }
  77. int ncount = 0;
  78. for ( int i = 0; i < 100; i++)
  79. {
  80. Sleep(50);// 注释这个,会内存泄漏;
  81. unsigned int nThreadID = 0;
  82. HANDLE hThread = CreateThread(NULL, 0, ThreadWork, pInstance, 0, NULL);
  83. //HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadWork, pInstance, 0, &nThreadID);
  84. if (hThread)
  85. {
  86. ncount++;
  87. CString strNum = _T("");
  88. strNum.Format(_T("----创建线程成功: %d--------\n"), ncount);
  89. CloseHandle(hThread);
  90. //_endthreadex(hThread);
  91. }
  92. }
  93. system("pause");
  94. printf("\t\t %d \n\n\n",nThreadCount);
  95. pInstance->Release(); // 必须保证线程全部执行完成后才可以结束!
  96. system("pause");
  97. }
  98. return nRetCode;
  99. }