Qt Creator:在再次运行之前退出应用程序的上一个实例?
每次我在 Qt Creator 中运行我的项目时,它都会启动我的应用程序的另一个实例。我必须手动退出该应用程序,否则我的 Dock 很快就会满。多么痛苦啊。有办法解决这个问题吗?如果当我再次运行应用程序时,我可以终止已经运行的版本,那就更有意义了。这可以做到吗?
Every time I run my project in Qt Creator, it spins up another instance of my app. I have to manually quit the app or else my Dock gets full pretty fast. What a pain. Is there a way around this? It would make a lot more sense if when I run the app again, I could just terminate the already running version. Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用共享内存来解决您的问题。当已经有一个正在运行的实例时,我已经使用这种方法不启动程序的另一个实例。实际上我已经实现了这个来实现所谓的单实例应用程序。
然而,您的情况与我的情况有点不同,您需要以某种方式从第二个应用程序向第一个应用程序发送信号以使其关闭。我认为您仍然可以使用 QSharedMemory 来实现此行为。
我为实现单实例应用程序所做的就是创建一个具有通用唯一 ID 的共享内存(UUID) 作为密钥,每次我的程序启动时都会对其加锁,因此如果它已经被锁定,我的程序就会知道已经有一个正在运行的实例并自动关闭。
您需要改进此实现以适应您的要求。理论上,您需要做的是将函数指针(或 qt 信号)放入共享内存,当另一个实例出现时,使您的(第二个)实例触发该函数,强制退出第一个实例。不幸的是,我不知道如何实现这个,但我希望这能给你一个意见...
流程应该有点像下面这样:
为了给你一个轻微的提示,我的单实例代码如下所示,
祝你好运,不要'不要忘记在这里分享你的解决方案;)
编辑:
如果放置一个函数指针或 Qt 信号然后触发它是不可能的(我希望不会),你可以将一个变量放置到共享内存中,让我们说保存正在运行的实例数,并在您的应用程序中定期(在线程中)检查它并如果大于 1,则关闭应用程序。
请注意这里的比赛条件!您可以通过放置每个实例生成的一对随机数和开始时间来避免竞争条件。因此,在关闭之前,您的应用程序会确保它是较旧的应用程序。例如:
QPair
you can use shared memory to solve your problem. I have used this approach to not start another instance of my program while there is already a running instance. Actually I have implemented this to achieve so called single instance application.
However your case is a bit different from mine, you need to somehow send a signal from 2nd application to the first one to make it close. I think you can achieve this behaviour still using
QSharedMemory
.What I've done to achive single instance app is to create a shared memory with a universally unique id(UUID) as key and every time my program starts put a lock on it, so if it is already locked my program understands there is already a running instance and closes automatically.
You need to improve this implementation to adapt your requirement. In theory what you need to do is to put a function pointer(or a qt signal) to the shared memory and when another instance come up make your (second) instance fire that function which forces to exit the first instance. Unfortunately I don't know how to implement this but I hope this would give you an opinion...
The flow should be somewhat like follows:
To give you a slight hint, my code for single instance is like follows
good luck and don't forget to share your solution here ;)
EDIT:
If putting a function pointer or Qt signal and then fire it is impossible(I hope not) you can put a variable to shared memory to lets say hold the number of running instances and in your app periodically(in a thread) check it and if it is greater than 1 close the application.
Watch out for race conditions here! You can avoid race conditions by putting a pair of a random number generated by each instance and start time. so before closing, your app ensures that it is the elder one. Ex:
QPair<int, QDateTime>