Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated Single Process Option of Wails app #1351

Closed
Purple-CSGO opened this issue Apr 25, 2022 · 5 comments
Closed

Integrated Single Process Option of Wails app #1351

Purple-CSGO opened this issue Apr 25, 2022 · 5 comments
Labels
Enhancement New feature or request Good First Issue Good for newcomers TODO The issue is ready to be developed

Comments

@Purple-CSGO
Copy link

Purple-CSGO commented Apr 25, 2022

Describe the solution you'd like

Currently wails does not have integrated option to make sure only 1 process is running at a time. Pls consider add this feature to wails. Thx.

@Purple-CSGO Purple-CSGO added the Enhancement New feature or request label Apr 25, 2022
@leaanthony leaanthony added this to the v2.5.0 milestone Jun 22, 2022
@KiddoV
Copy link
Contributor

KiddoV commented Jul 7, 2022

In the meantime, you can do something like this, not sure if it is a good method, but it seem to work for me.

// Create a mutex to keep a program running on single instance. Return error if program is already running.
func CreateMutex(name string) (uintptr, error) {
	kernel32 := syscall.NewLazyDLL("kernel32.dll")
	procCreateMutex := kernel32.NewProc("CreateMutexW")
	ret, _, err := procCreateMutex.Call(0, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(name))))
	switch int(err.(syscall.Errno)) {
	case 0:
		return ret, nil
	default:
		return ret, err
	}
}

Call the function in the main.go just before app := NewApp().

import (
    sysWindows "golang.org/x/sys/windows"
)
// Create mutex to keep app running in single instance
if _, mutexErr := CreateMutex("YourAppName"); mutexErr != nil {
        // Do whatever... for me, I call a messagebox...
	sysWindows.MessageBox(
		sysWindows.GetShellWindow(),
		syscall.StringToUTF16Ptr("Application is already running!"),
		syscall.StringToUTF16Ptr("Warning"),
		sysWindows.MB_OK|sysWindows.MB_ICONEXCLAMATION|sysWindows.MB_SYSTEMMODAL) //See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw for details
	os.Exit(0) //Exit if the program is already running
}

@leaanthony
Copy link
Member

I'd love to integrate this into the options as a callback. Thoughts?

@avengerweb
Copy link
Contributor

file locks is one of ways to go
https://github.com/postfinance/single

@leaanthony leaanthony added Good First Issue Good for newcomers TODO The issue is ready to be developed labels Nov 12, 2022
@NanoNik
Copy link
Contributor

NanoNik commented Nov 12, 2022

There's another way making this on windows - Events.
This way your first process will know that new process is launched, and, for eaxample, bring your main window to front instead of openning second process.

CreateEventW
SetEvent
WaitForSingleObject

It will look something like this (in C, but porting it to Go is pretty straightforward):

HANDLE hEvent = CreateEventW(nullptr, false, false, L"Global\<appname>");
if (GetLastError() == ERROR_ALREADY_EXISTS) {
    // If process is already running, fire event and exit
    SetEvent(hEvent);
    CloseHandle(hEvent);
    ExitProcess(0);
}

// If no processes is running, just wait for events
while (true) {
    DWORD result = WaitForSingleObject(hEvent, INFINITE);
    if (result == WAIT_OBJECT_0) {
        // New process is launched
        // Bring your main window to front or show a message
    }
}

@leaanthony leaanthony removed this from the v2.5.0 milestone Dec 29, 2022
@stffabi stffabi mentioned this issue Mar 11, 2023
2 tasks
@leaanthony
Copy link
Member

leaanthony commented Aug 3, 2023

This has been implemented as a v3 plugin. Windows + Mac currently supported.
EDIT: Linux too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Good First Issue Good for newcomers TODO The issue is ready to be developed
Projects
None yet
Development

No branches or pull requests

5 participants