rasim Posted July 23, 2008 Share Posted July 23, 2008 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.au3expandcollapse popup#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 EndFuncmuttley Link to comment Share on other sites More sharing options...
Andreik Posted July 23, 2008 Share Posted July 23, 2008 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. Link to comment Share on other sites More sharing options...
ptrex Posted July 23, 2008 Share Posted July 23, 2008 @rasimVery nice ! muttley @AndreikMaybe 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 Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
Andreik Posted July 23, 2008 Share Posted July 23, 2008 @ptrex Thank you very much for hint. Now I know how can I use mutex. muttley When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Cha0sBG Posted July 23, 2008 Share Posted July 23, 2008 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 More sharing options...
rasim Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for feedback guys! muttley Link to comment Share on other sites More sharing options...
mlowery Posted July 23, 2008 Share Posted July 23, 2008 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 More sharing options...
wraithdu Posted July 23, 2008 Share Posted July 23, 2008 (edited) Search -http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspxPS -I believe the correct value for INFINITE is 0xFFFFFFFF which should be the same as -1. Edited July 23, 2008 by wraithdu Link to comment Share on other sites More sharing options...
rasim Posted July 24, 2008 Author Share Posted July 24, 2008 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 expandcollapse popup#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 More sharing options...
Andreik Posted July 24, 2008 Share Posted July 24, 2008 Thanks for example. Now I understood useful of mutex. muttley When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now