📄 subject_64443.htm
字号:
<blockquote><p>
回复者:badboy 回复日期:2003-12-11 12:25:00
<br>内容:实在不好意思! 他的做法我试过了!不行! 我实在找不出问题所在!<BR> 源代码我贴在上面了! 请帮我看看!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:dsadsdas 回复日期:2003-12-11 12:45:26
<br>内容:俺们公司他不允许俺们用winrar。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-11 13:15:52
<br>内容:那我只能挨个文件发给您看了! 希望得到您的帮助!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-11 13:17:26
<br>内容:贴!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-11 13:19:18
<br>内容:不行啊! 论坛只接受zip.rar jpg<BR> 所以,我发到你邮箱里去了!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-11 20:50:50
<br>内容://下面的是.c文件<BR>/************************************************************<BR>Module name: CritSecs.C<BR>Notices: Copyright (c) 1995-1997 Jeffrey Richter<BR>************************************************************/<BR><BR><BR>#include "..\CmnHdr.H" /* See Appendix C. */<BR>#include <windows.h><BR>#include <windowsx.h><BR>#include <tchar.h><BR>#include <stdio.h> // For sprintf<BR>#include <process.h> // For _beginthreadex<BR>#include "Resource.H"<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>// Global variables<BR>// g_fTerminate is set to TRUE when the dialog box is<BR>// dismissed. It indicates to the worker threads that they <BR>// need to terminate. It is volatile because it can change<BR>// at any time.<BR>volatile BOOL g_fTerminate = FALSE; <BR><BR>HWND g_hwnd;<BR>HANDLE g_hThread[2]; // Counter[0] & Display[1] threads<BR><BR><BR>// The data that needs protecting<BR>TCHAR g_szNumber[10] = __TEXT("0");<BR><BR>// The critical section used to protect the data<BR>CRITICAL_SECTION g_CriticalSection;<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>// Add a string to a list box.<BR>void AddToListBox (LPCTSTR szBuffer) {<BR> HWND hwndDataBox = GetDlgItem(g_hwnd, IDC_DATABOX);<BR><BR> int x = ListBox_AddString(hwndDataBox, szBuffer);<BR> ListBox_SetCurSel(hwndDataBox, x);<BR><BR> if (ListBox_GetCount(hwndDataBox) > 100)<BR> ListBox_DeleteString(hwndDataBox, 0);<BR>}<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>// Thread to increment the protected counter data<BR>DWORD WINAPI CounterThread (LPVOID lpThreadParameter) {<BR> unsigned int nNumber, nDigit;<BR> BOOL fSyncChecked;<BR><BR> while (!g_fTerminate) {<BR> // Get the status of the Synchronize check box<BR> // and save it.<BR> fSyncChecked = <BR> IsDlgButtonChecked(g_hwnd, IDC_SYNCHRONIZE);<BR><BR> if (fSyncChecked) {<BR> // If the user wants us synchronized, do it.<BR> EnterCriticalSection(&g_CriticalSection);<BR> }<BR><BR> // Convert the string number to an integer and add 1.<BR> _stscanf(g_szNumber, __TEXT("%d"), &nNumber);<BR> nNumber++;<BR><BR> // Convert the new integer back to a string.<BR> nDigit = 0;<BR> while (nNumber != 0) {<BR> // Put a digit into the string.<BR> g_szNumber[nDigit++] = (TCHAR) <BR> (__TEXT('0') + (nNumber % 10));<BR><BR> // A call to Sleep here tells the system that we want<BR> // to relinquish the remainder of our time slice to<BR> // another thread. This call is needed for <BR> // single-CPU systems so that the results of the <BR> // synchronization or lack thereof are obvious. <BR> // Normally, your programs would NOT call Sleep here.<BR> Sleep(0);<BR><BR> // Get ready to get the next digit.<BR> nNumber /= 10;<BR> }<BR><BR> // All digits converted to characters.<BR> // Terminate the string.<BR> g_szNumber[nDigit] = 0;<BR><BR> // Characters were generated in reverse order;<BR> // reverse the string.<BR> // Call _strrev if ANSI, Call _wcsrev if Unicode.<BR> _tcsrev(g_szNumber);<BR><BR> if (fSyncChecked) {<BR> // If the user wants synchronization, do it.<BR> // In earlier versions of this program, I was calling<BR> // IsDlgButtonChecked as I did earlier instead of <BR> // using the fSyncChecked variable. This caused <BR> // problems because the user could check or uncheck <BR> // the Synchronize check box in between the calls to <BR> // EnterCriticalSection and LeaveCriticalSection.<BR> // This meant that my thread was sometimes leaving a<BR> // critical section that it had never entered. And my <BR> // thread was sometimes entering a critical section<BR> // that it had never left.<BR> LeaveCriticalSection(&g_CriticalSection);<BR> }<BR><BR> // If the user wants to display something <BR> // after each iteration, do it.<BR> if (IsDlgButtonChecked(g_hwnd, IDC_SHOWCNTRTHRD))<BR> AddToListBox(__TEXT("Cntr: Increment"));<BR> }<BR> return(0); // We get here when the window is dismissed.<BR>}<BR><BR><BR>///////////////////////////////////////////////////////////////<BR><BR><BR>// Thread to add the current value of <BR>// the counter (data) to the list box<BR>DWORD WINAPI DisplayThread (LPVOID lpThreadParameter) {<BR> BOOL fSyncChecked;<BR> TCHAR szBuffer[50];<BR><BR> while (!g_fTerminate) {<BR><BR> // Determine whether the user wants the threads<BR> // to be synchronized.<BR> fSyncChecked = <BR> IsDlgButtonChecked(g_hwnd, IDC_SYNCHRONIZE);<BR><BR> if (fSyncChecked)<BR> EnterCriticalSection(&g_CriticalSection);<BR><BR> // Construct a string with the string form of the number.<BR> _stprintf(szBuffer, __TEXT("Dspy: %s"), g_szNumber);<BR><BR> if (fSyncChecked)<BR> LeaveCriticalSection(&g_CriticalSection);<BR><BR> // Add the string form of the number to the list box.<BR> AddToListBox(szBuffer);<BR> }<BR> return(0); // We get here when the window is dismissed.<BR>}<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus, <BR> LPARAM lParam) {<BR> HWND hWndCtl;<BR> DWORD dwThreadID;<BR> <BR> // Associate an icon with the dialog box.<BR> chSETDLGICONS(hwnd, IDI_CRITSECS, IDI_CRITSECS);<BR><BR> // Save the handle of the dialog box in a global so that <BR> // the threads can easily gain access to it. This must be <BR> // done before creating the threads.<BR> g_hwnd = hwnd;<BR><BR> // Initialize the critical section. This must also be <BR> // done before any threads try to use it.<BR> InitializeCriticalSection(&g_CriticalSection);<BR><BR> // Create our counter thread and let it start running.<BR> g_hThread[0] = chBEGINTHREADEX(NULL, 0, <BR> CounterThread, NULL, 0, &dwThreadID);<BR><BR> // Create our display thread and let it start running.<BR> g_hThread[1] = chBEGINTHREADEX(NULL, 0, <BR> DisplayThread, NULL, 0, &dwThreadID);<BR><BR><BR> // Fill the Process Priority Class combo box and select<BR> // Normal.<BR> hWndCtl = GetDlgItem(hwnd, IDC_PRIORITYCLASS);<BR> ComboBox_AddString(hWndCtl, __TEXT("Idle"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("High"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Realtime"));<BR> ComboBox_SetCurSel(hWndCtl, 1); // Normal<BR><BR> // Fill the Display Thread Priority <BR> // combo box and select Normal.<BR> hWndCtl = GetDlgItem(hwnd, IDC_DSPYTHRDPRIORITY);<BR> ComboBox_AddString(hWndCtl, __TEXT("Idle"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Lowest"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Below normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Above normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Highest"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Timecritical"));<BR> ComboBox_SetCurSel(hWndCtl, 3); // Normal<BR><BR> // Fill the Counter Thread Priority <BR> // combo box and select Normal.<BR> hWndCtl = GetDlgItem(hwnd, IDC_CNTRTHRDPRIORITY);<BR> ComboBox_AddString(hWndCtl, __TEXT("Idle"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Lowest"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Below normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Above normal"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Highest"));<BR> ComboBox_AddString(hWndCtl, __TEXT("Timecritical"));<BR> ComboBox_SetCurSel(hWndCtl, 3); // Normal<BR><BR> return(TRUE);<BR>}<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>void Dlg_OnDestroy (HWND hwnd) {<BR> // When the dialog box is destroyed, signal the worker<BR> // threads to terminate.<BR> g_fTerminate = TRUE;<BR><BR> // Resume the worker threads in case they're paused.<BR> ResumeThread(g_hThread[0]);<BR> ResumeThread(g_hThread[1]);<BR>}<BR><BR><BR>//////////////////////////////////////////////////////////////<BR><BR><BR>void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl, <BR> UINT codeNotify) {<BR> <BR> HANDLE hThread;<BR> DWORD dw;<BR><BR> switch (id) {<BR> case IDCANCEL:<BR> EndDialog(hwnd, id);<BR> break;<BR><BR> case IDC_PRIORITYCLASS:<BR> if (codeNotify != CBN_SELCHANGE)<BR> break;<BR><BR> // User is changing priority class.<BR> switch (ComboBox_GetCurSel(hwndCtl)) {<BR> case 0:<BR> dw = IDLE_PRIORITY_CLASS;<BR> break;<BR><BR> case 1:<BR> default:<BR> dw = NORMAL_PRIORITY_CLASS;<BR> break;<BR><BR> case 2:<BR> dw = HIGH_PRIORITY_CLASS;<BR> break;<BR><BR> case 3:<BR> dw = REALTIME_PRIORITY_CLASS;<BR> break;<BR> }<BR> SetPriorityClass(GetCurrentProcess(), dw);<BR> break;<BR><BR> case IDC_DSPYTHRDPRIORITY:<BR> case IDC_CNTRTHRDPRIORITY:<BR> if (codeNotify != CBN_SELCHANGE)<BR> break;<BR><BR> switch (ComboBox_GetCurSel(hwndCtl)) {<BR> case 0:<BR> dw = (DWORD) THREAD_PRIORITY_IDLE;<BR> break;<BR><BR> case 1:<BR> dw = (DWORD) THREAD_PRIORITY_LOWEST;<BR> break;<BR><BR> case 2:<BR> dw = (DWORD) THREAD_PRIORITY_BELOW_NORMAL;<BR> break;<BR><BR> case 3:<BR> default:<BR> dw = (DWORD) THREAD_PRIORITY_NORMAL;<BR> break;<BR><BR> case 4:<BR> dw = (DWORD) THREAD_PRIORITY_ABOVE_NORMAL;<BR> break;<BR><BR> case 5:<BR> dw = (DWORD) THREAD_PRIORITY_HIGHEST;<BR> break;<BR><BR> case 6:<BR> dw = (DWORD) THREAD_PRIORITY_TIME_CRITICAL;<BR> break;<BR> }<BR> // User is changing the relative priority <BR> // of one of the threads.<BR> hThread = (id == IDC_CNTRTHRDPRIORITY) ?<BR> g_hThread[0] : g_hThread[1];<BR><BR> SetThreadPriority(hThread, dw);<BR> break;<BR><BR> case IDC_PAUSE:<BR> // User is pausing or resuming both threads.<BR> if (Button_GetCheck(hwndCtl)) {<BR><BR> SuspendThread(g_hThread[0]);<BR> SuspendThread(g_hThread[1]);<BR><BR> } else {<BR><BR> ResumeThread(g_hThread[0]);<BR> ResumeThread(g_hThread[1]);<BR><BR> }<BR> break;<BR> }<BR>}<BR><BR><BR>///////////////////////////////////////////////////////////////<BR><BR><BR>BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg, <BR> WPARAM wParam, LPARAM lParam) {<BR> <BR> switch (uMsg) {<BR> chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);<BR> chHANDLE_DLGMSG(hwnd, WM_DESTROY, Dlg_OnDestroy);<BR> chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);<BR> }<BR> return(FALSE);<BR>}<BR><BR><BR>///////////////////////////////////////////////////////////////<BR><BR><BR>int WINAPI _tWinMain (HINSTANCE hinstExe,<BR> HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {<BR><BR> chWARNIFUNICODEUNDERWIN95();<BR> DialogBox(hinstExe, MAKEINTRESOURCE(IDD_CRITSECS), <BR> NULL, Dlg_Proc);<BR><BR> // Wait for both worker threads to terminate <BR> WaitForMultipleObjects(2, g_hThread, TRUE, INFINITE);<BR><BR> // Close our handles to the worker threads<BR> CloseHandle(g_hThread[0]);<BR> CloseHandle(g_hThread[1]);<BR><BR> // The worker threads can no longer be using the <BR> // critical section, so delete it.<BR> DeleteCriticalSection(&g_CriticalSection);<BR><BR> return(0);<BR>}<BR><BR><BR>///////////////////////// End Of File /////////////////////////<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -