Jump to content

run one instance at a time


Recommended Posts

What i have is an outo it script that deals with email alerts that arive at a particular machine.

1. outlokk has a rule set to start an exe when it receives a new email

2. my Exe goes to the first email (the one it just received and then steps through and forwards the email to a specefic email, alters the subject, Prepends some info to the body, and then at the end it deletes the email.

The problem is quite often due to the equipment that send it the alert emails, it will receive two (or more) emails at the same time. so you end up with a situation where one script has not completed when the next one starts so it upsets the whole process.

What i want to do is check for other instances and wait until they finnish and then run in turn.

My thought was this

;little script to ensure only one process at a time

#Include<Array.au3>

;Find out number of sms.exe PID's and assign it to variable $sms_pids

$sms_pids = ProcessList("testsms.exe")

;Get the number of running sms.exe's and strip last one off because it is this instance

$number_of_pids = $sms_pids[0][0]

$extra_pids = $number_of_pids

;wait for the pids to close (except this instance)

If $number_of_pids > 0.1 Then

$count=1

While $count < $extra_pids

MsgBox(0,"Waiting","im waiting the count is "& $count,1)

ProcessWaitClose($sms_pids[$count][1])

$count=$count+1

WEnd

EndIf

;--------------------------------------------------------------------------

MsgBox(0,"go","im good to go")

Exit

Obviously there is other stuff after the exit and in production the msgboxes are stripped out.

This works fine if i test it by double clicking the exe twice to get two instances. However when outlook fires the multiples it doesnt work because they seem to fire off so fast that they are both waiting for each other. (I end up with two instances both in the waiting state)

Any ideas?

For your referance below is the code that appears after the top part which as i said forwards the email to an SMS provider that then SMS's the info to my mobile phone, (so sorry i have cut and replaced parts that list my account details.

--------------------Start------------------------------------

; Script Start - Add your code below here

Opt("WinWaitDelay",100)

Opt("SendKeyDelay", 50)

Opt("WinTitleMatchMode",4)

Opt("WinDetectHiddenText",1)

Opt("MouseCoordMode",0)

If Not WinActive("Inbox - Microsoft Outlook","") Then WinActivate("Inbox - Microsoft Outlook","")

WinWaitActive("Inbox - Microsoft Outlook","")

sleep(5000)

;goto first email in inbox

Send("{PGUP}")

sleep(1000)

;Forward email to SMS gateway

send("{CTRLDOWN}f{CTRLUP}")

sleep(5000)

send("xyz@domain.com ")

sleep(1000)

send("{TAB}{TAB}")

sleep(1000)

;send("{DEL}{DEL}{DEL}{DEL}{DEL}")

send("{DEL 5}")

sleep(1000)

send("61")

sleep(1000)

send("{ctrldown}a{CTRLUP}")

sleep(1000)

send("{ctrldown}c{CTRLUP}")

sleep(1000)

Send("{TAB}")

sleep(1000)

send("{SHIFTDOWN}{DOWN 8}{SHIFTUP}")

sleep(1000)

send("{DEL}")

sleep(1000)

send("{CTRLDOWN}f")

send("{CTRLUP}")

sleep(1000)

WinWait("Find and Replace","")

If Not WinActive("Find and Replace","") Then WinActivate("Find and Replace","")

WinWaitActive("Find and Replace","")

sleep(1000)

send("!m")

sleep(1000)

Send("{ALTDOWN}p")

send("{ALTUP}")

sleep(1000)

send("!e")

sleep(1000)

send("p")

sleep(1000)

send(@TAB)

sleep(1000)

send("!e")

sleep(1000)

send("p")

sleep(1000)

send("text:")

sleep(1000)

send("{ALTDOWN}a")

send("{ALTUP}")

sleep(1000)

WinWait("Microsoft Office Word","")

If Not WinActive("Microsoft Office Word","") Then WinActivate("Microsoft Office Word","")

WinWaitActive("Microsoft Office Word","")

Send("{ENTER}")

sleep(1000)

WinWait("Find and Replace","")

If Not WinActive("Find and Replace","") Then WinActivate("Find and Replace","")

WinWaitActive("Find and Replace","")

Send("{ESC}")

sleep(1000)

Send("api_id: xxxxxx"& @cr &"User: xxxxxxx"& @cr &"password: xxxxx"& @cr &"to:{ctrldown}v")

send("{ctrlup}")

send(" "& @cr &"text:")

send("!s")

sleep(5000)

;send reply to inform message sent

send("{CTRLDOWN}r{CTRLUP}")

sleep(5000)

;send("{shiftdown}")

send("+{tab}")

;send("{shiftup}")

sleep(1000)

send("{DEL 2}")

send("SMS sent to")

send("{tab}")

Opt("SendKeyDelay", 15)

send("Message sent")

sleep(1000)

send("!s")

sleep(5000)

;Delete original message

send("{DEL}")

---------------------------END-------------------------------

Link to comment
Share on other sites

  • Moderators

Check out SingleTon()

If it returns a value of it already being run then do a loop with sleep until that SingleTon value is showing it isn't there any longer.

Example:

#include <misc.au3>
If _Singleton('ThereCanBeOnlyOne', 1) = 0 Then
    Do
        Sleep(100)
    Until _Singleton('ThereCanBeOnlyOne', 1)
EndIf

Either of those would go at the top of your script, then you would compile to exe... in either case, it will sleep while the first script finishes it's business.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

OK so then i would have to have a script run a second script:

ie outlook start fire.exe

which in turn looks for send.exe running and if not running then it would launch send.exe otherwise it will sleep until it can.

It can t be within the one exe as then it is looking for itself, which of course will allways be there. If this is correct then i will give it a go.

Link to comment
Share on other sites

So jsut to clarify the line of script would look like

if _Singleton("test\test",1) = 0 Then

Blah Blah Blah ............

is that correct?

I think the example in the help file is quite clear, isn't it?

#include "Misc.au3"
if _Singleton("test\test",1) = 0 Then
    Msgbox(0,"Warning","An occurence of test is already running")
    Exit
EndIf
Msgbox(0,"OK","the first occurence of test is running")

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

These dont work

CODE
#include <misc.au3>

;If _Singleton('sms.exe', 1) = 0 Then

; Do

; Sleep(100)

; Until _Singleton('sms.exe', 1)=1

;EndIf

While _Singleton('sms.exe', 1) = 0

sleep(100)

MsgBox(0,"singlton value", _Singleton('sms.exe', 1))

WEnd

;If UBound(ProcessList(@sms)) - 1 >= 2 Then

; Do

; Sleep(100)

; Until UBound(ProcessList(@sms)) - 1 = 1

;EndIf

MsgBox (0,"Good","good to go")

Exit

above is the code for three differant attempts at the same thing. Compile this to a exe called sms.exe and then start the two instances of the exe. the first will complet to the good to go message the second one forever stays in the loop, even after the first instance has finnished and gone.

Link to comment
Share on other sites

any idea's

I thimk this could be a lot simpler if you can obtain a list af all the messages sitting in the message que. Use that kist to create an array amd them use

For $I = 1 To $MsgLst[0] Then

Do whatever

Next

Also never use things like

send("{ctrldown}a{CTRLUP}")

That should be send("!a")

Read the help file for more about send()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

You should only see Position 1 once with the original launch... the other launches will wait... be careful not to have more than two running at one time.

_WaitMyBrothersAndSisters(@ScriptName);Script must be compiled and ran for this to work
Dim $WMBASError = @error
While 1
    If $WMBASError = 0 Then
        ToolTip('This PID = ' & @AutoItPID & ' | Position 1', 0, 0)
    Else
        ToolTip('This PID = ' & @AutoItPID & ' | Position 2', 0, 40)
    EndIf
    Sleep(100)
WEnd

Func _WaitMyBrothersAndSisters($sExe)
    Local $nUboundPList = UBound(ProcessList($sExe)) - 1
    If $nUboundPList = 1 Then Return SetError(0, 0, 1)
    Do
        Sleep(100)
    Until UBound(ProcessList($sExe)) - 1 = 1
    Return SetError(1, 0, 1)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

i have solved this with the following

CODE
#include <misc.au3>

_Singleton('sms.exe', 0)

Do

;Insert code here

;Delete the email that you just processed in the code

until StatusbarGetText("Inbox") = "0 items"

Thankyou all for your help

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...