找回密码
 加入
搜索
查看: 2444|回复: 4

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

[复制链接]
发表于 2011-3-7 09:24:45 | 显示全部楼层 |阅读模式
本帖最后由 蜘蛛抱蛋 于 2011-9-28 21:02 编辑

比如用winhttp接受数据时,如果网络环境差,整个程序就卡在一条语句上,甚至无响应。有办法解决吗?比如执行simpleresponse时仍然能响应窗体,或者手动中断语句?
发表于 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[1] = "CSOTC" Then MemoWrite($aParam[2])
        If $aParam[1] = "DATA" Then
            MsgBox(0,'DONE',$aParam[2])
                $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
发表于 2011-3-7 17:42:00 | 显示全部楼层
呵呵,不错,等待更多高手指教
发表于 2011-3-7 23:46:51 | 显示全部楼层
winnet及socket都有非阻塞通讯的函数.
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-1 10:32 , Processed in 0.076173 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表