0

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"

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

4

8 回答 8

3

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


#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

}
于 2009-01-15T14:35:32.313 回答
3

如果这是 UNIX,您可以从 shell 运行程序,并在程序名称后添加一个与号“&”。

于 2009-01-15T10:51:57.537 回答
1

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.

于 2009-01-15T10:18:29.290 回答
1

如果您想要一个完全不使用控制台窗口或任何其他窗口的(非服务)程序,请将 main() 更改为 winmain()。不过,获取命令行参数有点复杂。

于 2009-01-15T16:58:26.767 回答
0

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

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

于 2009-01-15T13:39:13.347 回答
0

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

但是,您尚未指定为什么要这样做,因此这可能不适合您的情况。

于 2009-01-15T13:44:47.517 回答
0

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.

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.

于 2009-01-15T10:20:16.680 回答
-1

正确的方法是使用 Windows 服务,Google 上有足够的信息来说明如何做到这一点。

于 2009-01-15T23:53:23.047 回答