views:

994

answers:

9

Hi,

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?

+1  A: 

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.

Schildmeijer
A: 

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.

Rekreativc
+1  A: 

I see you have been trying to make-console-program-to-dissapear-and-work-in-background

Is this the same question rephrased?

Prakash
+3  A: 

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

Tony Lambert
A: 

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.

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

Luc Touraille
A: 

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.

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

Dynite
+1  A: 

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

}
lsalamon
+1  A: 

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.

Rob K
A: 

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

TomWij