Jump to content
Sign in to follow this  
rasim

File operations synchronizing

Recommended Posts

rasim

Hi, this is just little example, which are shows how to synchronize the file operations.

WriteData.au3 - writes. ReadData.au3 - reads only after WriteData.au3 are writes.

Note: use compiled scripts.

WriteData.au3

#include <GuiConstantsEx.au3>

Global $hMutex

GUICreate("WriteData", 200, 100)

$input = GUICtrlCreateInput("", 10, 10, 180, 20)

$buttonWrite = GUICtrlCreateButton("Write", 10, 70, 75, 23)

$buttonMutex = GUICtrlCreateButton("Mutex", 115, 70, 75, 23)
GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState()

_CreateMutex("WriteData")

While 1
    Switch GUIGetMsg()
        Case -3
            ExitLoop
        Case $buttonWrite
            GUICtrlSetState($buttonWrite, $GUI_DISABLE)
            GUICtrlSetState($buttonMutex, $GUI_ENABLE)
            _Write()
        Case $buttonMutex
            GUICtrlSetState($buttonWrite, $GUI_ENABLE)
            GUICtrlSetState($buttonMutex, $GUI_DISABLE)
            _CreateMutex("WriteData")
    EndSwitch
WEnd

Func _CreateMutex($sOccurName)
    $hMutex = DllCall("kernel32.dll", "hwnd", "CreateMutex", "int", 0, "int", 1, "str", $sOccurName)
    $hMutex = $hMutex[0]
    If $hMutex = 0 Then Exit
EndFunc

Func _Write()
    FileWriteLine("c:\write.txt", GUICtrlRead($input))
    DllCall("kernel32.dll", "int", "ReleaseMutex", "hwnd", $hMutex)
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hMutex)
    $hMutex = 0
EndFunc

muttley

Share this post


Link to post
Share on other sites
Andreik

Thanks for sharing. It`s a good idea. Give me an example where I can use this scripts? muttley


When the words fail... music speaks

Share this post


Link to post
Share on other sites
ptrex

@rasim

Very nice ! muttley

@Andreik

Maybe this can give you a hint.

In computer programming, a mutex (mutual exclusion object) is a program object that is created so that multiple program thread can take turns sharing the same resource, such as access to a file. Typically, when a program is started, it creates a mutex for a given resource at the beginning by requesting it from the system and the system returns a unique name or ID for it. After that, any thread needing the resource must use the mutex to lock the resource from other threads while it is using the resource. If the mutex is already locked, a thread needing the resource is typically queued by the system and then given control when the mutex becomes unlocked (when once more, the mutex is locked during the new thread's use of the resource).

Regards,

ptrex

Share this post


Link to post
Share on other sites
Andreik

@ptrex

Thank you very much for hint. Now I know how can I use mutex. muttley


When the words fail... music speaks

Share this post


Link to post
Share on other sites
Cha0sBG

nice script man muttley


Have Questions About GUI (Graphical User Interface) ? Post Them Here :GUI Help And Support ForumHave Questions About General AutoIt ? Post Them Here : General Help And Support ForumNew To AutoIt ? Be Shure To Check Out The FaQ's (Frequently Asked Questions) Or FaQ ยน There You May Find Great Help That Will Guide You True The Wonderful Programming Language AutoItOthere Good Place To Get Some Knolage Of AutoIt Is The Example Script ForumNotice A Bug ? Please Go And Report it At Bug Report Section And Help The Devolepers Of AutoIt Update And Fix The Programming LanguageWant To Thank The People For This Great Forum And Programming Language ? Then DonateWhen You Found The Answer Your Looking For Please Add [Resolved] To The Thread's Name That Will Show Otheres That You Have Found What Your Looking For And They Whount Have To Enter The Thread.

Share this post


Link to post
Share on other sites
rasim

Thanks for feedback guys! muttley

Share this post


Link to post
Share on other sites
mlowery

Out of curiosity, what happens to a mutex if not released? (For example, if the Write.au3 script completely crashed for some reason before the file operation was complete.) At some point, the Read.au3 script could give up, but will mutex fragments pile up in memory or something? I have a few script "sets" that could benefit from using a system-level flag to stay in sync, but sometimes one of them will fail for reasons outside my control, and I'd want to clean up gracefully.

Relatedly, is the INFINITE parameter related to some auto-expiration of the mutex? If so, how does that part work?

Share this post


Link to post
Share on other sites
rasim

mlowery

what happens to a mutex if not released?

2-nd program (ReadData) will not work, because WaitForSingleObject

MSDN

Waits until the specified object is in the signaled state or the time-out interval elapses.

wraithdu

I believe the correct value for INFINITE is 0xFFFFFFFF which should be the same as -1.

Yes, you are right muttley

Andreik

Give me an example where I can use this scripts?

Ok, for example, in 1-st program (WriteData) we need to open txt-file for write, in 2-nd program (ReadData) we need to open txt-file for read. To avoid conflicts we using mutex:

WriteData

#include <GuiConstantsEx.au3>

Opt("GuiOnEventMode", 1)

Global $hMutex = _CreateMutex("WriteData")

GUICreate("WriteData", 200, 100)
GUISetOnEvent(-3, "_Exit")

$input = GUICtrlCreateInput("", 10, 10, 180, 20)

$buttonWrite = GUICtrlCreateButton("Write", 10, 70, 75, 23)
GUICtrlSetOnEvent(-1, "_Write")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _CreateMutex($sOccurName)
    $aRet = DllCall("kernel32.dll", "hwnd", "CreateMutex", "int", 0, "int", 1, "str", $sOccurName)
    If $aRet[0] = 0 Then Exit
    Return $aRet[0]
EndFunc

Func _Write()
    Local $hFile = FileOpen("c:\write.txt", 2)
    FileWriteLine("c:\write.txt", GUICtrlRead($input))
    FileClose($hFile)
    
    DllCall("kernel32.dll", "int", "ReleaseMutex", "hwnd", $hMutex)
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hMutex)
    Sleep(100)
    $hMutex = _CreateMutex("WriteData")
EndFunc

Func _Exit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
Andreik

Thanks for example. Now I understood useful of mutex. muttley


When the words fail... music speaks

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  

×