蜘蛛抱蛋 发表于 2011-3-7 09:24:45

联网假死能解决吗?[已解决]

本帖最后由 蜘蛛抱蛋 于 2011-9-28 21:02 编辑

比如用winhttp接受数据时,如果网络环境差,整个程序就卡在一条语句上,甚至无响应。有办法解决吗?比如执行simpleresponse时仍然能响应窗体,或者手动中断语句?

m765555 发表于 2011-3-7 13:14:39

顶起,我也想知道答案,

蜘蛛抱蛋 发表于 2011-3-7 16:21:13

回复 2# m765555


    好吧,来个土办法,但是不太稳定
#include <WinHttp.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "CoProc.au3"
Global $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")
Global $Button_1, $Button_2, $Button_3, $iMemo, $msg
Global $start, $status, $data='正在执行...'
GUICreate("独立进程下载")
Opt("GUICoordMode", 2)
$Button_1 = GUICtrlCreateButton("暂停", 30, 30, 100)
$Button_2 = GUICtrlCreateButton("显示", 0, -1)
$Button_3 = GUICtrlCreateButton("开始下载", 0, -1)
$iMemo = GUICtrlCreateEdit("", -300, 10, 300, 250)

GUISetState()
_CoProcReciver("Reciver")
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        ExitLoop
                Case $msg = $Button_1
                        If GUICtrlRead($Button_1) = '暂停' Then
                          _ProcSuspend($start)   ;没有效果似乎不能中断正在执行的语句,而只能暂停函数
                                GUICtrlSetData($Button_1,'继续')
                        Else
                                _ProcResume($start)
                                GUICtrlSetData($Button_1,'暂停')
                        EndIf
                Case $msg = $Button_2
            MsgBox(0,'',$data)
                Case $msg = $Button_3
                        GUICtrlSetData($iMemo, '')
                        $data='正在执行...'
                        $start = _CoProc("_load")
        EndSelect
WEnd
Exit

Func Reciver($vParameter)
        Dim $aParam = StringSplit($vParameter,'|')
        If $aParam = "CSOTC" Then MemoWrite($aParam)
        If $aParam = "DATA" Then
          MsgBox(0,'DONE',$aParam)
                $data='下载完成!'
        EndIf
EndFunc

Func _load()
        $hWINHTTP_STATUS_CALLBACK = DllCallbackRegister("__WINHTTP_STATUS_CALLBACK", "none", "handle;dword_ptr;dword;ptr;dword")
    Local $hOpen = _WinHttpOpen(Default)
    _WinHttpSetStatusCallback($hOpen, $hWINHTTP_STATUS_CALLBACK)
    Local $hConnect = _WinHttpConnect($hOpen, "autoitx.com")
    Local $data = _WinHttpSimpleRequest($hConnect)
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    DllCallbackFree($hWINHTTP_STATUS_CALLBACK)
        _CoProcSend($gi_CoProcParent, 'DATA'&'|'&$data)
EndFunc

Func __WINHTTP_STATUS_CALLBACK($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)
    #forceref $hInternet, $iContext, $pStatusInformation, $iStatusInformationLength
    Local $sStatus
    Switch $iInternetStatus
      Case $WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
            $sStatus = "Closing the connection to the server"
      Case $WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
            $sStatus = "Successfully connected to the server."
      Case $WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
            $sStatus = "Connecting to the server."
      Case $WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
            $sStatus = "Successfully closed the connection to the server."
      Case $WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
            $sStatus = "Data is available to be retrieved with WinHttpReadData."
      Case $WINHTTP_CALLBACK_STATUS_HANDLE_CREATED
            $sStatus = "An HINTERNET handle has been created."
      Case $WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
            $sStatus = "This handle value has been terminated."
      Case $WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
            $sStatus = "The response header has been received and is available with WinHttpQueryHeaders."
      Case $WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
            $sStatus = "Received an intermediate (100 level) status code message from the server."
      Case $WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
            $sStatus = "Successfully found the IP address of the server."
      Case $WINHTTP_CALLBACK_STATUS_READ_COMPLETE
            $sStatus = "Data was successfully read from the server."
      Case $WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
            $sStatus = "Waiting for the server to respond to a request."
      Case $WINHTTP_CALLBACK_STATUS_REDIRECT
            $sStatus = "An HTTP request is about to automatically redirect the request."
      Case $WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
            $sStatus = "An error occurred while sending an HTTP request."
      Case $WINHTTP_CALLBACK_STATUS_REQUEST_SENT
            $sStatus = "Successfully sent the information request to the server."
      Case $WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
            $sStatus = "Looking up the IP address of a server name."
      Case $WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
            $sStatus = "Successfully received a response from the server."
      Case $WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
            $sStatus = "One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from the server."
      Case $WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
            $sStatus = "Sending the information request to the server."
      Case $WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
            $sStatus = "The request completed successfully."
      Case $WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
            $sStatus = "Data was successfully written to the server."
    EndSwitch
        _CoProcSend($gi_CoProcParent, 'CSOTC'&'|'&$iInternetStatus & " " &$sStatus)
EndFunc

Func MemoWrite($sMessage = "")
        GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc

m765555 发表于 2011-3-7 17:42:00

呵呵,不错,等待更多高手指教

ceoguang 发表于 2011-3-7 23:46:51

winnet及socket都有非阻塞通讯的函数.
页: [1]
查看完整版本: 联网假死能解决吗?[已解决]