PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap20/BigJob2/BigJob2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 238 lines | 182 code | 46 blank | 10 comment | 10 complexity | a00205ff862ebf2f47ec69695bea0d6a MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module BigJob2;
  6. /+
  7. + Warning: This app keeps running in the background after exit.
  8. + Probably something to do with WaitForSingleObject.
  9. +
  10. +/
  11. import core.memory;
  12. import core.runtime;
  13. import core.thread;
  14. import std.concurrency;
  15. import std.conv;
  16. import std.math;
  17. import std.range;
  18. import std.string;
  19. import std.utf;
  20. auto toUTF16z(S)(S s)
  21. {
  22. return toUTFz!(const(wchar)*)(s);
  23. }
  24. pragma(lib, "gdi32.lib");
  25. pragma(lib, "comdlg32.lib");
  26. pragma(lib, "winmm.lib");
  27. import core.sys.windows.windef;
  28. import core.sys.windows.winuser;
  29. import core.sys.windows.wingdi;
  30. import core.sys.windows.winbase;
  31. import core.sys.windows.commdlg;
  32. import core.sys.windows.mmsystem;
  33. alias win32.winuser.MessageBox MessageBox;
  34. string appName = "BigJob2";
  35. string description = "Multithreading Demo";
  36. HINSTANCE hinst;
  37. extern (Windows)
  38. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  39. {
  40. int result;
  41. try
  42. {
  43. Runtime.initialize();
  44. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  45. Runtime.terminate();
  46. }
  47. catch (Throwable o)
  48. {
  49. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  50. result = 0;
  51. }
  52. return result;
  53. }
  54. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  55. {
  56. hinst = hInstance;
  57. HACCEL hAccel;
  58. HWND hwnd;
  59. MSG msg;
  60. WNDCLASS wndclass;
  61. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  62. wndclass.lpfnWndProc = &WndProc;
  63. wndclass.cbClsExtra = 0;
  64. wndclass.cbWndExtra = 0;
  65. wndclass.hInstance = hInstance;
  66. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  67. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  68. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  69. wndclass.lpszMenuName = appName.toUTF16z;
  70. wndclass.lpszClassName = appName.toUTF16z;
  71. if (!RegisterClass(&wndclass))
  72. {
  73. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  74. return 0;
  75. }
  76. hwnd = CreateWindow(appName.toUTF16z, // window class name
  77. description.toUTF16z, // window caption
  78. WS_OVERLAPPEDWINDOW, // window style
  79. CW_USEDEFAULT, // initial x position
  80. CW_USEDEFAULT, // initial y position
  81. CW_USEDEFAULT, // initial x size
  82. CW_USEDEFAULT, // initial y size
  83. NULL, // parent window handle
  84. NULL, // window menu handle
  85. hInstance, // program instance handle
  86. NULL); // creation parameters
  87. ShowWindow(hwnd, iCmdShow);
  88. UpdateWindow(hwnd);
  89. while (GetMessage(&msg, NULL, 0, 0))
  90. {
  91. TranslateMessage(&msg);
  92. DispatchMessage(&msg);
  93. }
  94. return msg.wParam;
  95. }
  96. enum REP = 10_000_000;
  97. enum STATUS_READY = 0;
  98. enum STATUS_WORKING = 1;
  99. enum STATUS_DONE = 2;
  100. enum WM_CALC_DONE = (WM_USER + 0);
  101. enum WM_CALC_ABORTED = (WM_USER + 1);
  102. struct PARAMS
  103. {
  104. HWND hwnd;
  105. HANDLE hEvent;
  106. BOOL bContinue;
  107. }
  108. // used to be volatile
  109. __gshared PARAMS params;
  110. void ThreadFunc()
  111. {
  112. double A = 1.0;
  113. INT i;
  114. LONG lTime;
  115. while (true)
  116. {
  117. WaitForSingleObject(params.hEvent, INFINITE);
  118. lTime = GetCurrentTime();
  119. for (i = 0; i < REP && params.bContinue; i++)
  120. A = tan(atan(exp(log(sqrt(A * A))))) + 1.0;
  121. if (i == REP)
  122. {
  123. lTime = GetCurrentTime() - lTime;
  124. PostMessage(params.hwnd, WM_CALC_DONE, 0, lTime);
  125. }
  126. else
  127. {
  128. PostMessage(params.hwnd, WM_CALC_ABORTED, 0, 0);
  129. }
  130. }
  131. }
  132. extern (Windows)
  133. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  134. {
  135. scope (failure) assert(0);
  136. static HANDLE hEvent;
  137. static INT iStatus;
  138. static LONG lTime;
  139. static string[] szMessage = ["Ready (left mouse button begins)",
  140. "Working (right mouse button ends)",
  141. "%s repetitions in %s msec"];
  142. HDC hdc;
  143. PAINTSTRUCT ps;
  144. RECT rect;
  145. string szBuffer;
  146. switch (message)
  147. {
  148. case WM_CREATE:
  149. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  150. params.hwnd = hwnd;
  151. params.hEvent = hEvent;
  152. params.bContinue = FALSE;
  153. spawn(&ThreadFunc);
  154. return 0;
  155. case WM_LBUTTONDOWN:
  156. if (iStatus == STATUS_WORKING)
  157. {
  158. MessageBeep(0);
  159. return 0;
  160. }
  161. iStatus = STATUS_WORKING;
  162. params.bContinue = TRUE;
  163. SetEvent(params.hEvent);
  164. InvalidateRect(hwnd, NULL, TRUE);
  165. return 0;
  166. case WM_RBUTTONDOWN:
  167. params.bContinue = FALSE;
  168. return 0;
  169. case WM_CALC_DONE:
  170. lTime = lParam;
  171. iStatus = STATUS_DONE;
  172. InvalidateRect(hwnd, NULL, TRUE);
  173. return 0;
  174. case WM_CALC_ABORTED:
  175. iStatus = STATUS_READY;
  176. InvalidateRect(hwnd, NULL, TRUE);
  177. return 0;
  178. case WM_PAINT:
  179. hdc = BeginPaint(hwnd, &ps);
  180. GetClientRect(hwnd, &rect);
  181. szBuffer = format(szMessage[iStatus], REP, lTime);
  182. DrawText(hdc, szBuffer.toUTF16z, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  183. EndPaint(hwnd, &ps);
  184. return 0;
  185. case WM_DESTROY:
  186. PostQuitMessage(0);
  187. ExitProcess(0);
  188. return 0;
  189. default:
  190. }
  191. return DefWindowProc(hwnd, message, wParam, lParam);
  192. }