如何使 Flutter 桌面应用程序成为单实例?

问题描述 投票:0回答:0

我有一个 Flutter 桌面应用程序,想防止它的多个实例同时运行。为此,我尝试使用

Mutex
中的
package:mutex
,但它不起作用。任何人都可以为我更正它或告诉我任何其他方法吗?

这是我的代码:

import 'package:mutex/mutex.dart';
import 'dart:io';

void main() async {
  final mutex = Mutex();
  if (await mutex.acquire() == null) {
    runApp(MyApp());
    mutex.release();
  } else {
    // The mutex is already held by another instance of the app.
    // You can choose to show an error message or just exit silently.
    exit(0);
  }
}
flutter dart instance mutex
© www.soinside.com 2019 - 2024. All rights reserved.