简体   繁体   English

如何在C ++或Qt中创建进程?

[英]How can I create a process in C++ or Qt?

hello people a need your help, can be with qt or c++, i want to create a process BUT a process that use a function of my program not an external program, something like this .... sorry for the example and forgive my english thanks 你好,人们需要你的帮助,可以与qt或c ++一起使用,我想创建一个进程,但是要使用我程序的功能而不是外部程序的进程,像这样的事情....对不起这个例子,请原谅我的英语谢谢

void count(){blablabla}
int main(){QProcess p = new QProcess(count());p.start();}

IMHO, the easiest way would be to use threads. 恕我直言,最简单的方法是使用线程。 If your only objection to using thread is that the thread will be terminate, you can overcome it by waiting for the thread to finish before exiting the process. 如果您对使用线程的唯一反对意见是线程将被终止,则可以通过退出线程之前等待线程完成来克服它。

void doSomething();

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
    ...
    // Start a doSomething() in another thread using pure C++11
    std::thread cppThread(&doSomething);
    // The same using Qt
    QFuture<void> qtThreadResult = QtConcurrent::run(&doSomething);
    ...
    int r = app.exec();
    ...
    // The Qt application is closed
    // We wait for the thread to complete
    cppThread.join();
    // The same using Qt
    qtThreadResult.waitForFinished();

    // Everything is finished, we can exit
    return r;
}

If you absolutely need to create another process you have 2 solutions, either you create 2 programs A and B, and you start B from A, or you have a single program and you need to provide a command line argument so it can change its behavior. 如果您绝对需要创建另一个进程,则可以有2个解决方案,或者创建2个程序A和B,然后从A开始B,或者只有一个程序,并且需要提供命令行参数,以便它可以更改其行为。 。

void doSomething();

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
    if (app.arguments().contains("--foo"))
    {
        doSomething();
        return 0;
    }
    else
    {
        return app.exec();
    }
}

You can then start the new process using Qt: 然后,您可以使用Qt启动新过程:

QProcess::startDetached("B");
QProcess::startDetached("A --foo");
QProcess::startDetached(QCoreApplication::applicationFilePath(), QStringList{"--foo"});

Using pure C++ it gets a little more complicated. 使用纯C ++会更加复杂。 You have system() on all platforms but it does not offer a lot of option if you need to manipulate the created process. 您在所有平台上都具有system() ,但是如果您需要操纵创建的过程,则不会提供很多选择。 Then you have platform specific solutions like execl() (& co.) on Linux and CreateProcess() on Windows. 然后,您将拥有特定于平台的解决方案,例如Linux上的execl() (&co。)和Windows上的CreateProcess()

Also note that this can get even more complicated if you need to share data between the 2 processes ; 另请注意,如果您需要在两个进程之间共享数据,这可能会变得更加复杂; you will need to play with shared memories or sockets or pipes or stdin/stdout... 您将需要玩共享内存或套接字或管道或stdin / stdout ...

You can use QTimer. 您可以使用QTimer。 This will emit the timeout() signal at constant intervals. 这将以恒定的间隔发出timeout()信号。 Example for one second timer, 一秒计时器的示例,

QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(count()));
    timer->start(1000);

From then on, the count() slot is called every second. 从那时起,每秒调用一次count()插槽。 QTimer QTimer

I've done this via QtConcurrent as follow: 我已经通过QtConcurrent完成了,如下所示:

1) declare the method, which should run on an own thread: 1)声明该方法,该方法应在自己的线程上运行:

void TestClass::startWorkerThread(int parameter1) {
    // do something
}

2) Start the method in a thread: 2)在线程中启动方法:

#include <QtConcurrent/QtConcurrent>
... 
QFuture<void> pFuture1 = QtConcurrent::run(this, &TestClass::startWorkerThread, parameter1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM