如何在打开另一个图像时关闭图像 - linux c ++

How to close an image at the opening of another - linux c ++

本文关键字:图像 linux 另一个      更新时间:2023-10-16

如果一切都不完美,我深表歉意;( 我正在用c ++做一个程序,当它收到传感器信息时,会显示一张全屏显示图片。 问题是,当我想从一个图像转到另一个图像时,它会打开一个新的feh,直到计算机崩溃的那一刻,因为它占用了所有内存......

如何使图像的打开关闭前一个图像?

这是我当前的命令行:

system("feh -F ressources/icon_communication.png&");

我必须指定我也触发声音,但没有问题,因为程序在声音结束时自动关闭:

system("paplay /home/pi/demo_ecran_interactif/ressources/swip.wav&");

尝试将其作为测试并有效!谢谢@paul-桑德斯!

#include <iostream>
#include <chrono>
#include <thread>
#include <unistd.h>
#include <signal.h>
using namespace std;
pid_t display_image_file (const char *image_file)
{
pid_t pid = fork ();
if (pid == -1)
{
std::cout << "Could not fork, error: " << errno << "n";
return -1;
}
if (pid != 0)    // parent
return pid;
// child
execlp ("feh", "-F", image_file, NULL); // only returns on failure
std::cout << "Couldn't exec feh for image file " << image_file << ", error: " << errno << "n";
return -1;
}
int main()
{
pid_t pid = display_image_file ("nav.png");
if (pid != -1)
{
std::this_thread::sleep_for (std::chrono::milliseconds (2000));
kill (pid, SIGKILL);
}
pid_t pid2 = display_image_file ("sms2.png");
}

因此,这里的目标(就您的测试程序而言(似乎是:

  • feh显示nav.png
  • 等待 2 秒
  • 关闭(该实例(feh
  • feh显示sms2.png

如果你能让测试程序做到这一点,那么你就会走上正轨(我不会担心我漂亮的小脑袋关于你的声音问题(因为今天这里是 30+ 度(,但是一旦你让测试程序运行正确,那么你可能就能弄清楚如何自己解决这个问题(。

因此,我在这里的代码中看到的两个问题:

  • 您没有努力关闭"FEH"的第一个实例
  • execlp()并没有像你认为的那样做(具体来说,它永远不会返回,除非它由于某种原因失败(。

所以我认为你需要做的是这样的(代码未经测试,甚至可能无法编译,你需要找出正确的头文件来 #include,但它至少应该让你开始(:

pid_t display_image_file (const char *image_file)
{
pid_t pid = fork ();
if (pid == -1)
{
std::cout << "Could not fork, error: " << errno << "n";
return -1;
}
if (pid != 0)    // parent
return pid;
// child
execlp ("feh", "-F", image_file, NULL); // only returns on failure
std::cout << "Couldn't exec feh for image file " << image_file << ", error: " << errno << "n";
return -1;
}
int main()
{
pid_t pid = display_image_file ("nav.png");
if (pid != -1)
{
std::this_thread::sleep_for (std::chrono::milliseconds (2000));
kill (pid, SIGKILL);
}
pid_t pid = display_image_file ("sms2.png");
// ...
}

这有帮助吗?