Jump to content

Singleton using Atom / Semaphore


UEZ
 Share

Recommended Posts

39 minutes ago, argumentum said:

hmmm, let's say that I did not call the "Singleton_Delete($iSingleton)" and wanted to run the script again, how do I clear a previous $iSingleton ?

You can use 

Global $iSingleton = Singleton()
Singleton_Delete(@extended)

to delete the atom.

But you cannot use this in you main script because how do you know if it was called the 1st time?

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I always use this:

 

Global $sAppName = 'My App'

_RunOnce($sAppName, _OnSecondRun)
MsgBox(64, @ScriptName, 'Run me again')

;Main code...

Func _OnSecondRun()
    MsgBox(48, @ScriptName, 'Second copy detected, exiting...', 3)
    Exit
EndFunc

Func _RunOnce($sAppName = '', $sCallback = '')
    Local $iOld_WTMM = Opt('WinTitleMatchMode', 3)
    Local $sTitle = @ScriptName & '_@#$%^&*_' & $sAppName
    
    If WinExists('[CLASS:AutoIt v3;TITLE:' & $sTitle & ']') Then
        If Not $sCallback Then
            Exit
        EndIf
        
        Call(FuncName($sCallback))
    EndIf
    
    AutoItWinSetTitle($sTitle)
    Opt('WinTitleMatchMode', $iOld_WTMM)
EndFunc

You can use it even as UDF, without do anything...

RunOnce.au3:

#include-once
#OnAutoItStartRegister '__RunOnce'

Func __RunOnce()
    Local $iOld_WTMM = Opt('WinTitleMatchMode', 3)
    Local $sTitle = @ScriptName & '_@#$%^&*_' & @ScriptName
    
    If WinExists('[CLASS:AutoIt v3;TITLE:' & $sTitle & ']') Then
        MsgBox(48, @ScriptName, 'Second copy detected, exiting...', 3)
        Exit
    EndIf
    
    AutoItWinSetTitle($sTitle)
    Opt('WinTitleMatchMode', $iOld_WTMM)
EndFunc

Just add #include <RunOnce.au3> to your script.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

I added another Singleton variant using Semaphore winapi function. Check first post.

@MrCreatoR: thanks for your contribution.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

The semaphore one better fit my coding style**. :D
A note on the advantages of one over the other at the first post will be welcomed by those that code like me.

**good luck running this unstructured thoughtless rambling called code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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

×
×
  • Create New...