Jump to content
Sign in to follow this  
DeathSpree

Multi-Threading Problem

Recommended Posts

DeathSpree

Hello, I got a working Multi-Threading Script from> here

And now I changed the script for my use

#include <Process.au3>

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetOnEvent(-13, "FExit")

Global $IP = "203.117.124."
Global $Port = 7456
Global $ServerList
Global $Percent = 0.7874015748031496
Global $CurrentPercent = 0

Const $SCKTCPPROTOCOL = 0
Const $SCKUDPPROTOCOL = 1
Const $SCKCONNECTED = 7
Const $SCKERROR = 9

Main()

Func Main()
    Local $CurrentIP, $hThread, $Struct
    
    $hThread = DllCallbackRegister("CheckThread", "int", "ptr")

    TCPStartup()
    SplashTextOn("GGServerFinder", "Finding Working Servers - 0%", 250, 44, Default, Default, 1)

    For $i = 0 To 127
    
    $Struct = DllStructCreate("int Num;")
    DllStructSetData($Struct, 1, $i)
    CreateThread($hThread, $Struct)
        
    Next

    TCPShutdown()
    SplashOff()
    $Struct = 0 ;Free the allocated memory
    MsgBox(64, "Working Servers", $ServerList)

EndFunc ;==> Main

Func CheckThread($Data)
    Local $Pointer, $Num, $Socket
    $Num = DllStructGetData($Data, "Num")

    $CurrentPercent += $Percent
    ControlSetText("GGServerFinder", "", "Static1", "Finding Working Servers - " & String(Round($CurrentPercent)) & "%")
    
    $CurrentIP = $IP & String($Num)
    $Socket = _TCPConnect( $CurrentIP, $Port, 1000 )
    
    If (@error) Then Return
    
    $ServerList &= $CurrentIP & @CRLF
        
    TCPCloseSocket($Socket)
EndFunc   ;==> CheckThread

Func _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
    Local $hWs2 = DllOpen("Ws2_32.dll")
    Local $iDllErr, $fError = False, $aRes
    Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6)
    If @error Then
        $iDllErr = 3
    ElseIf $hSock[0] = 4294967295 Or $hSock[0] = -1 Then
        $fError = True
    Else
        $hSock = $hSock[0]
        $aRes = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr)
        If @error Or $aRes[0] = -1 Or $aRes[0] = 4294967295 Then
            $iDllErr = 1
        Else
            $iPort = DllCall($hWs2, "ushort", "htons", "ushort", $iPort)
            If @error Then
                $iDllErr = 2
            Else
                $iPort = $iPort[0]
            EndIf
        EndIf
        If 0 = $iDllErr Then
            Local $tSockAddr = DllStructCreate("short sin_family;ushort sin_port; ulong sin_addr;char sin_zero[8];")
            DllStructSetData($tSockAddr, 1, 2)
            DllStructSetData($tSockAddr, 2, $iPort)
            DllStructSetData($tSockAddr, 3, $aRes[0])

            If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")

            If $iTimeOut > -1 Then DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 1)
            $aRes = DllCall($hWs2, "int", "connect", "int", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))

            Select
                Case @error
                    $iDllErr = 4
                Case $aRes[0] <> 0
                    $aRes = DllCall($hWs2, "int", "WSAGetLastError")
                    If Not @error And $aRes[0] = 10035 Then ContinueCase
                    $fError = True
                Case $iTimeOut > -1
                    If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")
                    Local $t = DllStructCreate("uint;int")
                    DllStructSetData($t, 1, 1)
                    DllStructSetData($t, 2, $hSock)
                    Local $to = DllStructCreate("long;long")
                    DllStructSetData($to, 1, Floor($iTimeOut / 1000))
                    DllStructSetData($to, 2, Mod($iTimeOut, 1000))
                    $aRes = DllCall($hWs2, "int", "select", "int", $hSock, "ptr", DllStructGetPtr($t), "ptr", DllStructGetPtr($t), "ptr", 0, "ptr", DllStructGetPtr($to))
                    If Not @error And $aRes[0] = 0 Then
                        $aRes = DllCall($hWs2, "int", "WSAGetLastError")
                        If Not @error And $aRes[0] = 0 Then
                            $iDllErr = 10060
                        Else
                            $fError = True
                        EndIf
                    Else
                        DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 0)
                    EndIf
            EndSelect
        EndIf
    EndIf
    If $iDllErr Then
        TCPCloseSocket($hSock)
        $hSock = -1
    ElseIf $fError Then
        $iDllErr = DllCall($hWs2, "int", "WSAGetLastError")
        If Not @error Then $iDllErr = $iDllErr[0]
        If $iDllErr = 0 Then $iDllErr = 5
        TCPCloseSocket($hSock)
        $hSock = -1
    EndIf
    DllClose($hWs2)
    Return SetError($iDllErr, 0, $hSock)
EndFunc ;==> _TCPConnect

Func CreateThread($Handle, $struct)
    $return = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, "long", DllCallbackGetPtr($Handle), "ptr", DllStructGetPtr($struct), "long", 0, "int*", 0)
    Return $return[0]
EndFunc ;==> CreateThread

Func FExit()
    Exit
EndFunc ;==> FExit

The problem is that I get couple of errors in runtime

I think I'm not correctly using DllStructSetData & DllStructCreate or just using (not using) them in the right place

Thanks in advance

Share this post


Link to post
Share on other sites
FireFox

Hi,

AutoIt does not support multithreading.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
water

Here's a list of things AutoIt will never do: One position on this list is Multithreading


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
DeathSpree

Well , I know that autoit that will never add multi threading feature and it's not officially supported

BUT, There is some trick to do this and it posted on here

And other places on the forum

And IF you are really (100% sure) that what I'm going to do is impossible feel free to close this thread

Share this post


Link to post
Share on other sites
FireFox

It's a simple MsgBox, but still if you read the note there is an issue.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×