如何在后台执行 C++ 程序直到计算机关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21643933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:44:55  来源:igfitidea点击:

How to make a C++ program process in background until the computer's shut down?

c++windows

提问by Lucie kulza

  • Windows :

    1. Is it possible to make a C++ program executing in background without any display of Console or GUI ? And, How ?

    2. How do I to make it process until the computer's shut down ?

    3. How do I maintain a function executing while this program is active, please?

  • 窗户:

    1. 是否可以在不显示任何控制台或 GUI 的情况下使 C++ 程序在后台执行?如何 ?

    2. 我该如何处理直到计算机关闭?

    3. 请问如何在该程序处于活动状态时维护正在执行的函数?

IDE : Visual Studio 2013

IDE:Visual Studio 2013

回答by Bernd Elkemann

Step 1:If you are using an IDE then during project-creation it will most likely ask 'console-app' vs. 'window-app'. Choose window-app, which means that it will start without opening a console.

第 1 步:如果您使用的是 IDE,那么在项目创建期间,它很可能会询问“console-app”与“window-app”。选择window-app,这意味着它会在不打开控制台的情况下启动。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Step 2:Now within the code your IDE probably will have generated some code that makes a Window visible. Remove that code:

第 2 步:现在在代码中,您的 IDE 可能会生成一些使窗口可见的代码。删除该代码:

enter image description here

在此处输入图片说明

Your program now runs but is not visible on the task-bar:.

您的程序现在可以运行,但在任务栏上不可见:

enter image description here

在此处输入图片说明

For running your own code you have 2 options.Which of them is appropriate depends on your situation, but the second one is generally preferred:

要运行您自己的代码,您有 2 个选项。其中哪一个合适取决于您的情况,但通常首选第二个:

  • either use one of the generated methods like WinMain to start your own method which should contain a loop and within that loop your code plus a call to Sleep().

  • use windows-messages to run some of your code on-demand. (preferred)

  • 要么使用生成的方法之一,如 WinMain 来启动您自己的方法,该方法应包含一个循环,并在该循环中您的代码加上对 Sleep() 的调用。

  • 使用 windows-messages 按需运行一些代码。(首选

The program will run until your computer is shut down; then it will no-longer run.

该程序将一直运行到您的计算机关闭为止;那么它将不再运行。



A few notes on when to use option 1 vs. option 2:

关于何时使用选项 1 与选项 2 的一些说明:

Option 2is what is typically considered better because it works withthe operating system (Windows), it only executes code when the OS tells it that something changed. Option 1on the other hand does not depend on windows messages - sometimes you need this independence. It comes at a price though: your code will probably 'manually' check if something changed, sometimes do something, but most of the time choosing to Sleep(). This is called ->pollingbtw. so prefer Option 2.

选项2就是通常被认为是更好,因为它的工作原理操作系统(Windows)中,它只有在OS告诉它什么改变执行代码。另一方面,选项 1不依赖于 Windows 消息 - 有时您需要这种独立性。不过它是有代价的:你的代码可能会“手动”检查是否有变化,有时会做一些事情,但大多数时候选择 Sleep()。这称为-> 轮询顺便说一句。所以更喜欢选项2。



And this is how to modify WndProcfor option-2-apps. Example: make a beep every second.

这就是为 option-2-apps修改 WndProc的方法。示例:每秒发出一声哔哔声。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message)
    {
    #define IDT_TIMER1 123 // todo find out which numbers are allowed
                           // which depends on windows AND your organization
    case WM_CREATE:
        SetTimer(hWnd, IDT_TIMER1, 1000, (TIMERPROC) NULL);
    break;
    case WM_TIMER: 
        switch (wParam) { 
        case IDT_TIMER1:
            Beep(100,50);
        break;
        }
    break;
    case WM_COMMAND:
    ...

回答by ST3

For that purpose you need to hide your window.

为此,您需要隐藏您的窗口。

For console: ShowWindow (GetConsoleWindow(), SW_HIDE);

对于控制台: ShowWindow (GetConsoleWindow(), SW_HIDE);

For Win32 project: either do not create window using CreateWindowor CreateWindowEx, or ShowWindow(hWnd, SW_HIDE)

对于 Win32 项目:要么不使用CreateWindow或创建窗口CreateWindowEx,要么ShowWindow(hWnd, SW_HIDE)



Or the best solution, you can create service, some sample

或者最好的解决方案,你可以创建服务,一些样本

回答by Muhammad Bin Ali

This is how you can hide the window:

这是隐藏窗口的方法:

void main() {
    HWND window;
    AllocConsole();
    window = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(window, 0);
    //close program when Esc pressed
    if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
        return 0;
    }
}