如何藉由建立第二個執行緒控制連線逾時值

重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。

按一下這裡查看此文章的英文版本:224318
依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。
結論
這個 acticle 顯示藉由建立第二個執行緒上設定逾時值 InternetSetOption API Bug 因應措施。
InternetSetOption 並不會設定逾時值
其他相關資訊
下列範例程式碼控制項的等待連線到 FTP 伺服器在 Wininet 多久。它會建立一個背景工作執行緒來呼叫封鎖 Wininet API。如果連線花更多的時間比指定的等候逾時時間值,原始執行緒會呼叫 InternetCloseHandle 放開封鎖 Wininet 函式。HTTP 通訊會套用相同的概念。在 HTTP 的情況下實際連線發生 HttpSendRequest 呼叫中。
   #include <windows.h>   #include <wininet.h>   #include <iostream.h>   DWORD WINAPI WorkerFunction( LPVOID );    HINTERNET g_hOpen, g_hConnect;   typedef struct    {       CHAR* pHost;       CHAR* pUser;       CHAR* pPass;   } PARM;   void main()   {       CHAR    szHost[] = "localhost";       CHAR    szUser[] = "JoeB";       CHAR    szPass[] = "test";       CHAR    szLocalFile[] = "localfile";       CHAR    szRemoteFile[] = "remotefile";       DWORD   dwExitCode;       DWORD   dwTimeout;       PARM    threadParm;       g_hOpen = 0;       if ( !( g_hOpen = InternetOpen ( "FTP sample",                                         LOCAL_INTERNET_ACCESS,                                         NULL,                                         0,                                         0 ) ) )       {                    cerr << "Error on InternetOpen: " << GetLastError() << endl;           return ;       }       // Create a worker thread        HANDLE   hThread;        DWORD    dwThreadID;       threadParm.pHost = szHost;       threadParm.pUser = szUser;       threadParm.pPass = szPass;       hThread = CreateThread(                     NULL,            // Pointer to thread security attributes                      0,               // Initial thread stack size, in bytes                      WorkerFunction,  // Pointer to thread function                      &threadParm,     // The argument for the new thread                     0,               // Creation flags                      &dwThreadID      // Pointer to returned thread identifier                  );           // Wait for the call to InternetConnect in worker function to complete       dwTimeout = 5000; // in milliseconds       if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )       {           cout << "Can not connect to server in "                 << dwTimeout << " milliseconds" << endl;           if ( g_hOpen )   InternetCloseHandle ( g_hOpen );           // Wait until the worker thread exits           WaitForSingleObject ( hThread, INFINITE );           cout << "Thread has exited" << endl;           return ;       }           // The state of the specified object (thread) is signaled       dwExitCode = 0;       if ( !GetExitCodeThread( hThread, &dwExitCode ) )       {           cerr << "Error on GetExitCodeThread: " << GetLastError() << endl;           return ;       }       CloseHandle (hThread);       if ( dwExitCode )       // Worker function failed          return ;           if ( !FtpGetFile ( g_hConnect,                           szRemoteFile,                          szLocalFile,                          FALSE,INTERNET_FLAG_RELOAD,                           FTP_TRANSFER_TYPE_ASCII,                          0 ) )       {           cerr << "Error on FtpGetFile: " << GetLastError() << endl;           return ;       }       if ( g_hConnect )           InternetCloseHandle( g_hConnect );       if ( g_hOpen )           InternetCloseHandle( g_hOpen );       return ;   }   /////////////////// WorkerFunction //////////////////////    DWORD WINAPI    WorkerFunction(       IN LPVOID vThreadParm   )   /*   Purpose:       Call InternetConnect to establish a FTP session     Arguments:       vThreadParm - points to PARM passed to thread   Returns:       returns 0     */    {       PARM* pThreadParm;       // Initialize local pointer to void pointer passed to thread       pThreadParm = (PARM*)vThreadParm;       g_hConnect = 0;           if ( !( g_hConnect = InternetConnect (                                g_hOpen,                                 pThreadParm->pHost,                                INTERNET_INVALID_PORT_NUMBER,                                pThreadParm->pUser,pThreadParm->pPass,                                INTERNET_SERVICE_FTP,                                 0,                                0 ) ) )       {           cerr << "Error on InternetConnnect: " << GetLastError() << endl;           return 1; // failure       }              return 0;  // success   }				

警告:本文為自動翻譯

內容

文章識別碼:224318 - 最後檢閱時間:06/29/2004 20:01:09 - 修訂: 2.2

Microsoft Internet Explorer 4.0 128-Bit Edition, Microsoft Internet Explorer 4.01 Service Pack 2, Microsoft Internet Explorer 4.01 Service Pack 1, Microsoft Internet Explorer 4.01 Service Pack 2, Microsoft Internet Explorer 5.0, Microsoft Internet Explorer 5.5

  • kbmt kbftp kbhowto kbhttp KB224318 KbMtzh
意見反應