How to use Mutex in V2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

How to use Mutex in V2

Post by viv » 21 Mar 2023, 08:20

I refer to the following link to write the test code
viewtopic.php?p=134940#p134940

V1 version of the re-run was able to exit correctly
But in the V2 version it exits but does not go to MsgBox(123) at all
I can't be sure if the exit is correct or caused by an error

V2:

Code: Select all

Persistent(true)
MutexName := "random"
If DllCall("OpenMutex", "Int", 0x100000, "Int", 0, "Str", MutexName)
    MsgBox(123),ExitApp()

hMutex := DllCall("CreateMutex", "Int", 0, "Int", False, "Str", MutexName)

hMutex := DllCall("CreateMutex", "Int", 0, "Int", False, "Str", MutexName)

MsgBox(hMutex)
OnExit((*) => DllCall("ReleaseMutex", "Ptr", hMutex))

V1:

Code: Select all

MutexName := "random"
If DllCall("OpenMutex", "Int", 0x100000, "Int", 0, "Str", MutexName)
{
    MsgBox 123
    ExitApp
}

hMutex := DllCall("CreateMutex", "Int", 0, "Int", False, "Str", MutexName)

OnExit((*) => DllCall("ReleaseMutex", "Ptr", hMutex))

Sleep, 1000000

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to use Mutex in V2

Post by swagfag » 21 Mar 2023, 09:24

one thing thats wrong is CreateMutex's LPSECURITY_ATTRIBUTES is a Ptrtype, not an Int

anyway, this thread doesnt make sense. what is "V1"? "version 1" of a script i ran? a script written in "ahk v1" (clearly not)?
explain:
  • what code ure running
  • how ure running it
  • what u think should happen
  • whats actually happening

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: How to use Mutex in V2

Post by viv » 21 Mar 2023, 09:43

@swagfag
v1 does refer to AHK v1

First run the program once

In AHK v1
The second re-run will go to MsgBox 123
and then exit
It indicates that the specified Mutex already exists
so the second run will go to MsgBox 123

In AHK V2
The second re-run does not go to MsgBox(123)
It exits directly
It means that the specified Mutex does not exist ?

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: How to use Mutex in V2  Topic is solved

Post by geek » 21 Mar 2023, 10:56

Your OnExit for the v1 is not correct, so the mutex is not freed in the v1 code. AHKv1 does not support fat arrow functions. Your use of => in v1 silently crashes the expression interpreter, causing it to exit the line without having done anything after that point in the expression.

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: How to use Mutex in V2

Post by viv » 21 Mar 2023, 12:31

@geek

Thank you very much
It's been a long time since I wrote the v1 code, and I forgot a lot of it.
Persistent(true) I have used sleep instead

I forgot to change the MutexName name...
Indeed it is occupied

Best wishes

Now I can't see the button to confirm the answer
I'll check back tomorrow

User avatar
cyruz
Posts: 346
Joined: 30 Sep 2013, 13:31

Re: How to use Mutex in V2

Post by cyruz » 22 Mar 2023, 05:49

I remembered I wrote a a class on V1 before: viewtopic.php?t=66454

It’s short, it should be trivial to translate it to V2.
ABCza on the old forum.
My GitHub.

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: How to use Mutex in V2

Post by viv » 23 Mar 2023, 05:23

I run it directly in vscode when testing
The extension is vscode-autohotkey2-lsp
It adds /ErrorStdOut=utf-8 to the parameters which may interfere with the functionality

Tips:
I realized it was posted in the wrong section.
Could please anyone move it to Ask for Help?
Last edited by gregster on 23 Mar 2023, 06:29, edited 1 time in total.
Reason: Moved topic.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to use Mutex in V2

Post by swagfag » 23 Mar 2023, 08:01

is that supposed to imply that something still isnt working?

because noone knows what code ure actually running. ur OP still shows nonsensical code. u even have self admitted to having errors in ur code. for all i care, u might have written even more errors in, while u were "fixing" them. its pointless for anybody to try and troubleshoot this for u.

if u have doubts about the extension, simple. dont use it. save an .ahk file and run with the interpreter manually

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: How to use Mutex in V2

Post by viv » 23 Mar 2023, 10:33

@swagfag

There is no error in the code, just an accidental duplication of a line

Add #SingleInstance off to the v2 code
and run it twice, it will do what I expect it to do
MsgBox(123),ExitApp()

I use it to confirm that the current script has a running instance
and then PostMessage to the running instance

PostMessage is not the point
So I replaced them with MsgBox(123),ExitApp()
As long as it runs here, it means it is correct

The problem has been solved by geek
Then at that time I did not find the post posted in the wrong place
So at that time it could not confirm the answer

Could please anyone move it to Ask for Help?

Then I asked the administrator to help move the post
Then confirm the correct answer

Post Reply

Return to “Ask for Help (v2)”