我需要一个后台程序C ++

时间:2022-10-30 09:14:17

I have created this program:

我创建了这个程序:

//Includes....
int main()
{
   ifstream readfile("File.txt");
   string str;
   while(1)
   {
      getline(readfile,str);
      system("cls");
      Sleep(10000);
   }
}

It's just a program that reads every 10 seconds a line from the file "File.txt"

它只是一个程序,每隔10秒从文件“File.txt”中读取一行

I want to make it work on background,how can I do that?

我想让它在背景上工作,我该怎么做?

8 个解决方案

#1


If this was UNIX you would run the program from the shell with an ampersand "&" after the program name.

如果这是UNIX,你将从shell运行程序,在程序名后面加上&符“&”。

#2


This sample hide console windows for you :

此示例为您隐藏控制台窗口:


#include "windows.h"
#include fstream 
#include string
#include stdio.h

using namespace std;


int main()
{
    WCHAR path[260];

    GetModuleFileName(NULL,path,260);

    HWND console = FindWindow(L"ConsoleWindowClass",path);

    if(IsWindow(console))

    ShowWindow(console,SW_HIDE); // hides the window

    //---------------------------------------------------

    ifstream readfile("File.txt");
    string str;
    while(1)
    {
        getline(readfile,str);
        system("cls");
        Sleep(10000);
    }

    //----------------------------------------------------

    if(IsWindow(console))

        ShowWindow(console,SW_SHOW); // shows the window

}

#3


Platform dependent. But I think you are using windows because of the "cls" command. Why cant you just start a new command prompt and execute the correct .exe file. Then just minimize the program and do what you are supposed to do. If want to automate the startup phase (like cron in linux/unix) use the built in scheduler for windows.

平台依赖。但我认为你正在使用Windows,因为“cls”命令。为什么你不能只是启动一个新的命令提示符并执行正确的.exe文件。然后只需最小化程序并执行您应该执行的操作。如果想要自动化启动阶段(如linux / unix中的cron),请使用内置的windows调度程序。

#4


If you want a (non-service) program that doesn't use a console window or any other windows at all, change main() to winmain(). Getting at command line arguments is a little more involved though.

如果您想要一个不使用控制台窗口或任何其他窗口的(非服务)程序,请将main()更改为winmain()。尽管如此,获取命令行参数还是要多一些。

#5


I'm sorry, but I'm not sure I know what you mean... as far as I know (please correct me if I'm wrong) there is no way to run a simple C(++) program without a console window. However if you want it to "sleep" in a way that uses near no resources, then that can be achieved by checking current time and compering it with the time when you completed your last read.

对不起,但我不确定我知道你的意思......据我所知(如果我错了请纠正我)没有办法运行一个简单的C(++)程序而没有控制台窗口。但是,如果您希望它以一种几乎不使用资源的方式“休眠”,那么可以通过检查当前时间并将其与您完成上次读取的时间进行比较来实现。

Sleep function usually uses more resources than this, however it works just as well, so if resource cap isn't very very important then it will do just as well.

睡眠功能通常使用比这更多的资源,但它也可以正常工作,所以如果资源上限不是很重要,那么它也会做得很好。

Hope this helped.

希望这有帮助。

#6


If you're launching your program from a Dos console, you can use the command start \B myprogram.exe, which is more or less equivalent to the unix ampersand. However, you'll need to keep the console open to keep the program running.

如果从Dos控制台启动程序,可以使用命令start \ B myprogram.exe,它或多或少等同于unix&符号。但是,您需要保持控制台处于打开状态以保持程序运行。

Another solution is to convert your application into a windows service, but then you'll have to rewrite (or wrap) your application.

另一个解决方案是将您的应用程序转换为Windows服务,但是您必须重写(或换行)您的应用程序。

#7


If you want this because you're logging some kind of output and want to view the changes you could just use an editor such as TextPad which will automatically reload the file when it changes.

如果你想要这样,因为你正在记录某种输出并希望查看更改,你可以使用TextPad等编辑器,它会在文件发生变化时自动重新加载。

However you've not specified why you want this so this may not be appropriate in your case.

但是,您没有说明为什么要这样,所以这可能不适合您的情况。

#8


The proper way is to use a Windows service, there is enough information on Google on how to do this.

正确的方法是使用Windows服务,Google上有足够的信息来说明如何执行此操作。

#1


If this was UNIX you would run the program from the shell with an ampersand "&" after the program name.

如果这是UNIX,你将从shell运行程序,在程序名后面加上&符“&”。

#2


This sample hide console windows for you :

此示例为您隐藏控制台窗口:


#include "windows.h"
#include fstream 
#include string
#include stdio.h

using namespace std;


int main()
{
    WCHAR path[260];

    GetModuleFileName(NULL,path,260);

    HWND console = FindWindow(L"ConsoleWindowClass",path);

    if(IsWindow(console))

    ShowWindow(console,SW_HIDE); // hides the window

    //---------------------------------------------------

    ifstream readfile("File.txt");
    string str;
    while(1)
    {
        getline(readfile,str);
        system("cls");
        Sleep(10000);
    }

    //----------------------------------------------------

    if(IsWindow(console))

        ShowWindow(console,SW_SHOW); // shows the window

}

#3


Platform dependent. But I think you are using windows because of the "cls" command. Why cant you just start a new command prompt and execute the correct .exe file. Then just minimize the program and do what you are supposed to do. If want to automate the startup phase (like cron in linux/unix) use the built in scheduler for windows.

平台依赖。但我认为你正在使用Windows,因为“cls”命令。为什么你不能只是启动一个新的命令提示符并执行正确的.exe文件。然后只需最小化程序并执行您应该执行的操作。如果想要自动化启动阶段(如linux / unix中的cron),请使用内置的windows调度程序。

#4


If you want a (non-service) program that doesn't use a console window or any other windows at all, change main() to winmain(). Getting at command line arguments is a little more involved though.

如果您想要一个不使用控制台窗口或任何其他窗口的(非服务)程序,请将main()更改为winmain()。尽管如此,获取命令行参数还是要多一些。

#5


I'm sorry, but I'm not sure I know what you mean... as far as I know (please correct me if I'm wrong) there is no way to run a simple C(++) program without a console window. However if you want it to "sleep" in a way that uses near no resources, then that can be achieved by checking current time and compering it with the time when you completed your last read.

对不起,但我不确定我知道你的意思......据我所知(如果我错了请纠正我)没有办法运行一个简单的C(++)程序而没有控制台窗口。但是,如果您希望它以一种几乎不使用资源的方式“休眠”,那么可以通过检查当前时间并将其与您完成上次读取的时间进行比较来实现。

Sleep function usually uses more resources than this, however it works just as well, so if resource cap isn't very very important then it will do just as well.

睡眠功能通常使用比这更多的资源,但它也可以正常工作,所以如果资源上限不是很重要,那么它也会做得很好。

Hope this helped.

希望这有帮助。

#6


If you're launching your program from a Dos console, you can use the command start \B myprogram.exe, which is more or less equivalent to the unix ampersand. However, you'll need to keep the console open to keep the program running.

如果从Dos控制台启动程序,可以使用命令start \ B myprogram.exe,它或多或少等同于unix&符号。但是,您需要保持控制台处于打开状态以保持程序运行。

Another solution is to convert your application into a windows service, but then you'll have to rewrite (or wrap) your application.

另一个解决方案是将您的应用程序转换为Windows服务,但是您必须重写(或换行)您的应用程序。

#7


If you want this because you're logging some kind of output and want to view the changes you could just use an editor such as TextPad which will automatically reload the file when it changes.

如果你想要这样,因为你正在记录某种输出并希望查看更改,你可以使用TextPad等编辑器,它会在文件发生变化时自动重新加载。

However you've not specified why you want this so this may not be appropriate in your case.

但是,您没有说明为什么要这样,所以这可能不适合您的情况。

#8


The proper way is to use a Windows service, there is enough information on Google on how to do this.

正确的方法是使用Windows服务,Google上有足够的信息来说明如何执行此操作。