Jump to content

Run Function as Seperate Process


WaitingForZion
 Share

  

12 members have voted

  1. 1. Do you think this is a good idea?

    • No
      5
    • Yes, but it has been presented before.
      1
    • Yes.
      6
  2. 2. Do you think I should have developed it further before posting it?

    • Eh, at least you got the start of it,
      5
    • Yeah, you really should have.
      7
  3. 3. Should I have kept this idea to myself?

    • Yes. Now others are going to finish it before you.
      0
    • It's already been done.
      6
    • No, it is a good idea and needs to be shared. Everyone has the right to their own implementation of common ideas.
      6


Recommended Posts

The following code creates a seperate script process for a specified function, and execute it. It is in it most rudimentary form, but I see it as the beginning to the solution of multi-threading. Others may have developed a similar solution. I'm not doubting that. However, I'd like to make this small contribution to make up for my earlier slandering of AutoIt and one of its developers.

If we can combine this functionality with some form of interprocess communication, we will arrive at a solution or work around for multi-threading. Instead of executing two threads in the same process from the same script (which is impossible), or executing two separate scripts, we can dynamically create a script from a given function, and execute it concurrently with the script that held and called the function.

I have not fully developed this. Like I said, it is in its most rudimentary form. But I want to open you to the idea, and to kindle an endeavor to implement it.

Also, like I said, it may have been implemented already. I neither knew if it was nor searched to find out. It would have spoiled the fun.

So tell me what you think.

#include <Array.au3>

func ScriptCreateFromFunc($function_name)
    $this_script = FileOpen(@ScriptFullPath, 0)
    $new_path = @TempDir & "\" & @ScriptName & "_" & $function_name
    $new_script = FileOpen($new_path, 2)
    Dim $lines[1]
    $lines[0] = 0

    if $this_script <> -1 and $new_script <> -1 then
        do
            $line = FileReadLine($this_script)
            $error = @error
            _ArrayAdd($lines, $line)
            $lines[0] = $lines[0] + 1
        until $error == -1
    Else
        FileClose($this_script)
        FileClose($new_script)
        return -1
    EndIf

    FileClose($this_script)

    $func_point = -1
    $end_point = -1

    for $i = 1 to $lines[0]
        $func_path = "Func " & $function_name
        $func_path_length = StringLen($func_path)

        if StringLen($lines[$i]) >= $func_path_length then
            $tmp_line = StringLower(StringLeft($lines[$i], $func_path_length))
            if StringLower($func_path) == $tmp_line Then
                $func_point = $i
                ExitLoop
            EndIf
        EndIf
    Next

    if $func_point <> -1 then
        for $i = $func_point + 1 to $lines[0]
            $tmp_line = StringLower($lines[$i])
            if $tmp_line == "endfunc" Then
                $end_point = $i
                ExitLoop
            EndIf
        next
    Else
        FileClose($new_script)
        return -1
    endif

    if $end_point <> -1 Then
        for $i = $func_point + 1 to $end_point - 1
            FileWriteLine($new_script, $lines[$i])
        Next
    Else
        FileClose($new_script)
        Return -2
    EndIf

    FileClose($new_script)

    return $new_path
EndFunc

func ScriptRun($scriptfile)
    Run(@AutoItExe & " " & $scriptfile)
endfunc

func myfunc()
    ;This is the function text.
    ;This is a function that will run.
    ;This is the function text.
    ;This function can be executed as a seperate process.
    Opt("WinTitleMatchMode", 2)
    Run("Notepad.exe")
    WinWait("Notepad")
    WinActivate("Notepad")
    Send("Yep, it worked!")
EndFunc

ScriptRun(ScriptCreateFromFunc("myfunc"))
Spoiler

"This then is the message which we have heard of him, and declare unto you, that God is light, and in him is no darkness at all. If we say that we have fellowship with him, and walk in darkness, we lie, and do not the truth: But if we walk in the light, as he is in the light, we have fellowship one with another, and the blood of Jesus Christ his Son cleanseth us from all sin. If we say that we have no sin, we deceive ourselves, and the truth is not in us. If we confess our sins, he is faithful and just to forgive us our sins, and to cleanse us from all unrighteousness. If we say that we have not sinned, we make him a liar, and his word is not in us." (I John 1:5-10)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...