Jump to content

File operations synchronizing


rasim
 Share

Recommended Posts

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

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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