推 Dforce: 純c win32不用static 我寫MFC當member function才用static 08/20 15:34
推 Dforce: 有沒有程式碼 這樣很難猜 08/20 15:39
→ BIAO: 我是用C++的類別來包...XD 08/20 15:40
→ BIAO: 謝謝D大... 目前已決定執行緒那圈不包在類別內! 08/20 22:35
推 kwpn: 用static member function可正常使用在CreateThread呀,建議 08/21 01:29
→ kwpn: 你貼出程式來,找出真的問題,而不是找到其他方式可以用就好 08/21 01:30
→ BIAO: 謝謝k大,那麼我稍晚再精簡整理一下喔! 08/21 18:22
for (;;)
{
//m_odbcHelper.RunSQL(L"Insert into [DataCenter].[dbo].T_ST_Account
values('QQ','835')");
_tprintf( TEXT("\nPipe Server: Main thread awaiting client connection
on %s\n"), lpszPipename);
hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BufferSize, // output buffer size
BufferSize, // input buffer size
0, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("CreateNamedPipe failed, GLE=%d.\n"),
GetLastError());
return -1;
}
// Wait for the client to connect; if it succeeds,
// the function returns a nonzero value. If the function
// returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
Data pTemp;
pTemp.hPipe = hPipe;
pTemp.lpMem = NULL;
if (fConnected)
{
printf("Client connected, creating a processing thread.\n");
// Create a thread for this client.
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
NamedPipeThreadProc, // thread proc
(LPVOID) &pTemp, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
return -1; }
else CloseHandle(hThread);
}
else
// The client could not connect, so close the pipe.
_tprintf(TEXT("CreateThread failed, GLE=%d.\n"), GetLastError());
CloseHandle(hPipe);
}
--
k大您好,此迴圈是在某類別的成員函式內,
將NamedPipeThreadProc設為static後,
每次執行緒調用成功後,藉由GetLastError除第1次為0外,其餘皆是印出183,
新執行緒有正常運作,但我擔心是否會有不預期的問題發生...
還請您不吝賜教,謝謝您。
※ 編輯: BIAO (36.230.203.201), 08/23/2015 01:37:14
推 kwpn: 你的code是來自msdn上的,我自己從msdn上copy code下來測試, 08/23 15:25
→ kwpn: msdn的code, 即使我把server改成class, thread函式改static 08/23 15:27
→ kwpn: 一樣可以正常work, 所以看不出你的問題到底在哪 08/23 15:28
→ kwpn: 另外你認為thread有問題, 應該另外寫個簡單程式不要含有的沒 08/23 15:31
→ kwpn: 的功能 08/23 15:32
→ BIAO: 謝謝k大的測試,我會再抽空進行檢視,主要是因先前有出現183 08/26 13:36
→ BIAO: 的錯誤代碼,讓我擔心是否自己建構有誤.. 08/26 13:37