• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

Keeping a background thread alive, even after the main application exits.

I need one of my program’s background thread to keep running (will self terminate when it finishes); even when the process’s main thread has terminated. I do not want/need to keep the application’s primary thread alive.

Example:
Main Application
Thread process big file (e.g. add 9 to each byte inside the file, and write it to another file)
End Main Application
2 minutes later, thread finishes processing the big file.
I am currently creating worker thread with MFC’s AfxBeginThread, it will need to work with W2k, Xp,Vista (locked down)
Thank you
0
jmc0
Asked:
jmc0
  • 3
  • 3
  • 2
  • +1
4 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
you need to create another process, not another thread, the simplest way to do it is to run another application.
0
 
evilrixSenior Software Engineer (Avast)Commented:
When the primary thread of a process terminates so to do all the other threads. You'll need to spawn another process if you want it to live beyond the parent. You can do this using the API function CreateProcess()

http://msdn2.microsoft.com/en-US/library/ms682425.aspx

-Rx.
0
 
jmc0Author Commented:
Already aware of the CreateProcess function and what happens when process terminates (all its thread terminates). That's why, I was hoping someone can provide a alternate simple solution, specifically to keeping thread alive, even if the main application has ended.
Currently my 'thread' code is not an application/process exe; which is required for the CreateProcess function (unless someone can tell me how to CreateProcess with just a pfnThreadProc 'Points to the controlling function for the worker thread') ; this is why I don't  create another process.
I need one of my program's background thread to keep running (will self terminate when it finishes); even when the process' main thread has terminated. I do not want/need to keep the application's primary thread alive.
 
Example:
Main Application
Thread process big file (e.g. add 9 to each byte inside the file, and write it to another file)
End Main Application
2 minutes later, thread finishes processing the big file.
I am currently creating worker thread with MFC's AfxBeginThread, it will need to work with W2k, Xp,Vista (locked down)
Thank you

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Jaime OlivaresSoftware ArchitectCommented:
You can simply hide your main window and wait for the secondary thread to terminate.
User will assume that application has ended, but it will be still processing in the background.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Put this function into another application (no/hidden main window) and launch this new application as Jaime initially suggested.
0
 
jmc0Author Commented:
None of the answer provides a 'real' solution to my problem.
0
 
jmc0Author Commented:
None of the answer does what I 'want' to do...
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> None of the answer does what I 'want' to do...
Unfortunately that's because what you want to do can't be done - at least not in the way you want to :(

If you are unhappy with the answers you should post a message to CS and ask then to delete the question and refund your points.

http://www.experts-exchange.com/Community_Support/General/

Sorry.

-Rx.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
An answer that is negative is still an answer.  (Would you have preferred no-one made any comments and you wasted many hours searching yourself?)

Why is it only worth a 'C' grade just because you would rather have had a positive answer?
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> An answer that is negative is still an answer.  (Would you have preferred no-one made any comments and you wasted many hours searching yourself?)
I agree! I wasn't saying we didn't give good answers, I was just pointing out that if the OP honestly felt we hadn't provided an appropriate response s/he could post a message to CS. It would, of course, then be down to a moderator to decide. Ultimately, the answers are right and I feel sure a moderator would clarify this!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now