Background Processes on Startup

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eboyjr14
    New Member
    • Apr 2007
    • 27

    Background Processes on Startup

    I want to have a program that runs ( shown below ) that doesn't show that little black window. ( as a background process ):

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    void startPro()
    {
         system("START C:/PROGRA~1/Yahoo!/Messenger/YahooMessenger.exe");
         system("START C:/PROGRA~1/MOZILL~1/firefox.exe");
         system("START C:/PROGRA~1/MICROS~2/Office/OUTLOOK.EXE");
    }
    void chkHour()
    {
        time_t rawtime;
        struct tm * timeinfo;
        char buffer [80];
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"%H",timeinfo);
        const char * outp = buffer;
        int hour = atoi(outp);
        if ( hour > 4 && hour < 13 ) startPro();
    }
    int main(int argc, char *argv[])
    {
        time_t rawtime;
        struct tm * timeinfo;
        char buffer [80];
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"%w",timeinfo);
        const char * outp = buffer;
        int day = atoi(outp);
        if ( day > 0 && day < 6 ) chkHour();
        return EXIT_SUCCESS;
    }
    What should I do?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Put it on its own thread so it runs in the background.

    If you are using Unix/Linus check out pcreate_thread.

    If Windows, you need CreateThread.

    They both work the same in principle. Your thread is a function. You pass the address of your function to pcreate_thread ot CreateThread. As long as the function is running, the thread is active.

    You will need slightly different protoypes between Unix/Linus and Windows.

    Comment

    • eboyjr14
      New Member
      • Apr 2007
      • 27

      #3
      Originally posted by weaknessforcats
      Put it on its own thread so it runs in the background.

      If you are using Unix/Linus check out pcreate_thread.

      If Windows, you need CreateThread.

      They both work the same in principle. Your thread is a function. You pass the address of your function to pcreate_thread ot CreateThread. As long as the function is running, the thread is active.

      You will need slightly different protoypes between Unix/Linus and Windows.
      I pretty new to C++ so I don't know how to do that. I have searched Google for an answer, but can't seem to find that. I am running it for Windows XP.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Essentially for Windows, you must have a function that is a ThreadProc. This is Microsoft-speak for this:
        [code=c]
        DWORD WINAPI ThreadProc(LPVO ID);
        [/code]

        That means your function must have an LPVOID argument and return a DWORD with a WINAPI declaration. These functions can be threads:

        [code=c]
        DWORD WINAPI MyFunction(LPVO ID);
        DWORD WINAPI MyWristWatch(LP VOID);
        //etc..
        [/code]

        All you do is call the Win32 CreateThread(). One of the arguments is a ThreadProc. It is here you specify the address of your function. Another argument will be the LPVOID (this is just a void*). The argument will show up in your thread when it starts.

        CreateThread() will start your function as a worker thread that will exist as long as your function is running.

        You use events to communicate between main() and your thread. If your thread function terminates after a while this will not be necessary. However, if it is in a infinite loop, you need to have your thread call WaitForSingleOb ject() to determine if it should continue.

        Just start reading MSDN starting with CreateThread(). There are plenty of topics linked together and these will surely get you going.

        Comment

        • eboyjr14
          New Member
          • Apr 2007
          • 27

          #5
          Originally posted by weaknessforcats
          Essentially for Windows, you must have a function that is a ThreadProc. This is Microsoft-speak for this:
          [code=c]
          DWORD WINAPI ThreadProc(LPVO ID);
          [/code]

          That means your function must have an LPVOID argument and return a DWORD with a WINAPI declaration. These functions can be threads:

          [code=c]
          DWORD WINAPI MyFunction(LPVO ID);
          DWORD WINAPI MyWristWatch(LP VOID);
          //etc..
          [/code]

          All you do is call the Win32 CreateThread(). One of the arguments is a ThreadProc. It is here you specify the address of your function. Another argument will be the LPVOID (this is just a void*). The argument will show up in your thread when it starts.

          CreateThread() will start your function as a worker thread that will exist as long as your function is running.

          You use events to communicate between main() and your thread. If your thread function terminates after a while this will not be necessary. However, if it is in a infinite loop, you need to have your thread call WaitForSingleOb ject() to determine if it should continue.

          Just start reading MSDN starting with CreateThread(). There are plenty of topics linked together and these will surely get you going.
          Awesome! Thxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxx!

          Comment

          Working...