Solved

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

Posted on 2007-11-18
10
1,002 Views
Last Modified: 2013-12-04
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
Comment
Question by:jmc0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 40 total points
ID: 20310806
you need to create another process, not another thread, the simplest way to do it is to run another application.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 90 total points
ID: 20311055
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
 

Author Comment

by:jmc0
ID: 20313244
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 40 total points
ID: 20313268
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
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 20 total points
ID: 20313545
Put this function into another application (no/hidden main window) and launch this new application as Jaime initially suggested.
0
 

Author Closing Comment

by:jmc0
ID: 31409893
None of the answer provides a 'real' solution to my problem.
0
 

Author Comment

by:jmc0
ID: 20346825
None of the answer does what I 'want' to do...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20346897
>> 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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 20347049
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20347266
>> 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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question